Skip to content

Commit 9bc56ac

Browse files
gabelevifacebook-github-bot
authored andcommitted
[Easy] LwtInit.run_lwt
Summary: Previously you needed two calls: one to set the engine and then one to run the lwt thread. This combines them into a single call. I originally built this because I was adding a third call. I eventually didn't add that third call, but still liked this idea better than what I had before. It also helps that xhpOfReactCommand.ml forgot to set the engine, which shows that this is useful. Reviewed By: samwgoldman Differential Revision: D7272136 fbshipit-source-id: 05c09ee42bc91b8ec169838318104ef2b7146059
1 parent 7dc659f commit 9bc56ac

File tree

4 files changed

+56
-53
lines changed

4 files changed

+56
-53
lines changed

src/common/lwt/lwtInit.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,8 @@ let set_engine () =
108108
* nowhere *)
109109
if Sys.win32
110110
then Lwt_engine.set (new windows_select) (* See comment on windows_select *)
111-
else Lwt_engine.set (new unix_select); (* See comment on unix_select *)
111+
else Lwt_engine.set (new unix_select) (* See comment on unix_select *)
112+
113+
let run_lwt f =
114+
set_engine ();
115+
Lwt_main.run (f ())

src/common/lwt/lwtInit.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* LICENSE file in the root directory of this source tree.
66
*)
77

8-
val set_engine: unit -> unit
8+
val run_lwt: (unit -> 'a Lwt.t) -> 'a

src/monitor/flowServerMonitor.ml

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -114,53 +114,53 @@ let internal_start ~is_daemon ?waiting_fd monitor_options =
114114

115115
(************************* HERE BEGINS THE MAGICAL WORLD OF LWT *********************************)
116116

117-
LwtInit.set_engine ();
118-
119-
Lwt.async (LogFlusher.run ~cancel_condition:ExitSignal.signal);
120-
121-
(* If `prom` in `Lwt.async (fun () -> prom)` resolves to an exception, this function will be
122-
* called *)
123-
Lwt.async_exception_hook := (fun exn ->
124-
let bt = Printexc.get_backtrace () in
125-
let msg = Utils.spf "Uncaught async exception: %s%s"
126-
(Printexc.to_string exn)
127-
(if bt = "" then bt else "\n"^bt)
117+
let initial_lwt_thread () =
118+
Lwt.async (LogFlusher.run ~cancel_condition:ExitSignal.signal);
119+
120+
(* If `prom` in `Lwt.async (fun () -> prom)` resolves to an exception, this function will be
121+
* called *)
122+
Lwt.async_exception_hook := (fun exn ->
123+
let bt = Printexc.get_backtrace () in
124+
let msg = Utils.spf "Uncaught async exception: %s%s"
125+
(Printexc.to_string exn)
126+
(if bt = "" then bt else "\n"^bt)
127+
in
128+
Logger.fatal ~exn "Uncaught async exception. Exiting";
129+
FlowExitStatus.(exit ~msg Unknown_error)
130+
);
131+
132+
Logger.init_logger
133+
?log_fd (Hh_logger.Level.min_level () |> lwt_level_of_hh_logger_level);
134+
Logger.info "argv=%s" (argv |> Array.to_list |> String.concat " ");
135+
136+
(* If there is a waiting fd, start up a thread that will message it *)
137+
let handle_waiting_start_command = match waiting_fd with
138+
| None -> Lwt.return_unit
139+
| Some fd ->
140+
let fd = Lwt_unix.of_unix_file_descr ~blocking:true fd in
141+
handle_waiting_start_command fd
128142
in
129-
Logger.fatal ~exn "Uncaught async exception. Exiting";
130-
FlowExitStatus.(exit ~msg Unknown_error)
131-
);
132-
133-
Logger.init_logger
134-
?log_fd (Hh_logger.Level.min_level () |> lwt_level_of_hh_logger_level);
135-
Logger.info "argv=%s" (argv |> Array.to_list |> String.concat " ");
136-
137-
(* If there is a waiting fd, start up a thread that will message it *)
138-
let handle_waiting_start_command = match waiting_fd with
139-
| None -> Lwt.return_unit
140-
| Some fd ->
141-
let fd = Lwt_unix.of_unix_file_descr ~blocking:true fd in
142-
handle_waiting_start_command fd
143-
in
144143

145-
(* Don't start the server until we've set up the threads to handle the waiting channel *)
146-
Lwt.async (fun () ->
147-
let%lwt () = handle_waiting_start_command in
148-
FlowServerMonitorServer.start monitor_options
149-
);
150-
151-
(* We can start up the socket acceptor even before the server starts *)
152-
Lwt.async (fun () ->
153-
SocketAcceptor.run
154-
(Lwt_unix.of_unix_file_descr ~blocking:true monitor_socket_fd)
155-
monitor_options.FlowServerMonitorOptions.autostop
156-
);
157-
Lwt.async (fun () ->
158-
SocketAcceptor.run_legacy (Lwt_unix.of_unix_file_descr ~blocking:true legacy_socket_fd)
159-
);
160-
161-
(* Wait forever! Mwhahahahahaha *)
162-
Lwt.wait () |> fst
163-
|> Lwt_main.run
144+
(* Don't start the server until we've set up the threads to handle the waiting channel *)
145+
Lwt.async (fun () ->
146+
let%lwt () = handle_waiting_start_command in
147+
FlowServerMonitorServer.start monitor_options
148+
);
149+
150+
(* We can start up the socket acceptor even before the server starts *)
151+
Lwt.async (fun () ->
152+
SocketAcceptor.run
153+
(Lwt_unix.of_unix_file_descr ~blocking:true monitor_socket_fd)
154+
monitor_options.FlowServerMonitorOptions.autostop
155+
);
156+
Lwt.async (fun () ->
157+
SocketAcceptor.run_legacy (Lwt_unix.of_unix_file_descr ~blocking:true legacy_socket_fd)
158+
);
159+
160+
(* Wait forever! Mwhahahahahaha *)
161+
Lwt.wait () |> fst
162+
in
163+
LwtInit.run_lwt initial_lwt_thread
164164

165165
let daemon_entry_point =
166166
FlowServerMonitorDaemon.register_entry_point (internal_start ~is_daemon:true)

src/server/server.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ let init_dfind options =
217217
let create_program_init ~shared_mem_config ~focus_targets options =
218218
let handle = SharedMem_js.init shared_mem_config in
219219
let genv = ServerEnvBuild.make_genv options handle in
220-
(* Only set up lwt after the workers are created *)
221-
LwtInit.set_engine ();
220+
222221
let program_init = fun () ->
223222
let%lwt profiling, env = init ~focus_targets genv in
224223
FlowEventLogger.init_done ~profiling;
@@ -233,7 +232,7 @@ let run ~monitor_channels ~shared_mem_config options =
233232

234233
let dfind = init_dfind options in
235234

236-
let initial_lwt_thread =
235+
let initial_lwt_thread () =
237236
(* Read messages from the server monitor and add them to a stream as they come in *)
238237
let listening_thread = listen_for_messages () in
239238

@@ -250,7 +249,7 @@ let run ~monitor_channels ~shared_mem_config options =
250249
serve ~dfind ~genv ~env
251250
]
252251
in
253-
Lwt_main.run initial_lwt_thread
252+
LwtInit.run_lwt initial_lwt_thread
254253

255254
let run_from_daemonize ~monitor_channels ~shared_mem_config options =
256255
try run ~monitor_channels ~shared_mem_config options
@@ -269,7 +268,7 @@ let check_once ~shared_mem_config ~client_include_warnings ?focus_targets option
269268
PidLog.disable ();
270269
MonitorRPC.disable ();
271270

272-
let initial_lwt_thread =
271+
let initial_lwt_thread () =
273272
let _, program_init =
274273
create_program_init ~shared_mem_config ~focus_targets options in
275274
let%lwt profiling, env = program_init () in
@@ -281,7 +280,7 @@ let check_once ~shared_mem_config ~client_include_warnings ?focus_targets option
281280
in
282281
Lwt.return (profiling, errors, warnings, suppressed_errors)
283282
in
284-
Lwt_main.run initial_lwt_thread
283+
LwtInit.run_lwt initial_lwt_thread
285284

286285
let daemonize =
287286
let entry = Server_daemon.register_entry_point run_from_daemonize in

0 commit comments

Comments
 (0)