Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

close descriptor when failed to open connection #97

Merged
merged 1 commit into from
May 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions lib/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,19 @@ let make_impl ~config ~conn_info fd =
let open Lwt_result.Syntax in
let* () = connect ~config ~conn_info fd in
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TLDR: If this line failed, that would lead to leak of descriptor

Log.info (fun m -> m "Connected to %a" Connection_info.pp_hum conn_info);
match conn_info.scheme with
| Scheme.HTTP ->
create_http_connection ~config fd
| HTTPS ->
create_https_connection ~config ~conn_info fd

let open_connection ~config conn_info =
let fd = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
if config.Config.tcp_nodelay then (
Lwt_unix.setsockopt fd Lwt_unix.TCP_NODELAY true;
Logs.debug (fun m -> m "TCP_NODELAY set"));
let open Lwt.Syntax in
let* impl =
match conn_info.scheme with
| Scheme.HTTP ->
create_http_connection ~config fd
| HTTPS ->
create_https_connection ~config ~conn_info fd
in
let* impl = make_impl ~config ~conn_info fd in
match impl with
| Ok _ ->
Lwt.return impl
Expand All @@ -182,13 +187,6 @@ let make_impl ~config ~conn_info fd =
let+ () = close_connection ~conn_info fd in
impl

let open_connection ~config conn_info =
let fd = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
if config.Config.tcp_nodelay then (
Lwt_unix.setsockopt fd Lwt_unix.TCP_NODELAY true;
Logs.debug (fun m -> m "TCP_NODELAY set"));
make_impl ~config ~conn_info fd

let change_connection t conn_info =
let open Lwt_result.Syntax in
let+ conn' = open_connection ~config:t.config conn_info in
Expand Down