Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 5c19a8d

Browse files
Merge pull request #9 from issuu/mku/no-deferred-in-thread
No deferred in thread
2 parents 3498f0d + f789172 commit 5c19a8d

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Remove `Deferred.t` from function running in the pool. Running async
2+
operations outside the main thread is not safe.
3+
14
0.11.0
25
======
36

src/thread_pool_async.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let init ~name ~(threads: int) ~(create: unit -> 'state) ~(destroy: 'state -> un
2828
type 'result computation = [`Ok of 'result | `Attempt_retry]
2929

3030
let with'
31-
: 'state t -> ?retries:int -> ('state -> 'result computation Deferred.t) -> 'result Deferred.Or_error.t
31+
: 'state t -> ?retries:int -> ('state -> 'result computation) -> 'result Deferred.Or_error.t
3232
= fun { pool = (reader, writer); create; destroy; _ } ?(retries=0) f ->
3333
assert (retries >= 0);
3434

@@ -40,7 +40,7 @@ let with'
4040
let run_once
4141
: 'thread -> 'state -> ('result computation Or_error.t * 'state) Deferred.t
4242
= fun thread state ->
43-
let%bind result = In_thread.run ~thread (fun () -> Deferred.Or_error.try_with (fun () -> f state)) |> Deferred.join in
43+
let%bind result = In_thread.run ~thread (fun () -> Or_error.try_with (fun () -> f state)) in
4444
match result with
4545
| Ok `Ok res -> return (Ok (`Ok res), state)
4646
| Ok `Attempt_retry ->

test/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(executable
22
(name test)
33
(preprocess (pps ppx_let))
4-
(libraries alcotest alcotest-async thread_pool_async core async_kernel async_unix))
4+
(libraries alcotest alcotest-async thread_pool_async core async_kernel))
55

66
(alias
77
(name runtest)

test/test.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
open Core
22
open Async_kernel
3-
open Async_unix
43

54
module Thread_pool = Thread_pool_async
65

@@ -16,8 +15,8 @@ let test_simple () =
1615
let create () = Int.incr states in
1716
let destroy () = Int.decr states in
1817
let worker () =
19-
let%bind () = after (Time.Span.of_ms 5.) in
20-
return @@ `Ok (Int.incr jobs)
18+
ignore @@ Unix.nanosleep 0.005;
19+
`Ok (Int.incr jobs)
2120
in
2221

2322
let%bind pool = Thread_pool.init ~name:"unittest" ~threads ~create ~destroy in
@@ -46,8 +45,8 @@ let test_error () =
4645
Int.incr errors;
4746
failwith "Stop"
4847
| _ ->
49-
let%bind () = after (Time.Span.of_ms 5.) in
50-
return @@ `Ok (Int.incr jobs)
48+
ignore @@ Unix.nanosleep 0.005;
49+
`Ok (Int.incr jobs)
5150
in
5251

5352
let%bind pool = Thread_pool.init ~name:"unittest" ~threads ~create ~destroy:ignore in
@@ -73,7 +72,7 @@ let test_retry () =
7372
| false -> `Ok (Int.incr work_done)
7473
in
7574
should_fail := false;
76-
return result
75+
result
7776
in
7877
let%bind pool = Thread_pool.init ~name:"unittest" ~threads ~create ~destroy in
7978
let%bind _result = List.init work_expected ~f:ignore |> Deferred.Or_error.List.iter ~how:`Parallel ~f:(fun () -> Thread_pool.with' ~retries:1 pool (worker (ref true))) in
@@ -92,7 +91,7 @@ let test_retry_fail () =
9291
let destroy = ignore in
9392
let worker () =
9493
Int.incr tries;
95-
return `Attempt_retry
94+
`Attempt_retry
9695
in
9796
let%bind pool = Thread_pool.init ~name:"unittest" ~threads ~create ~destroy in
9897
let%bind result = List.init work ~f:ignore |> Deferred.Or_error.List.iter ~how:`Parallel ~f:(fun () -> Thread_pool.with' ~retries:0 pool worker) in

0 commit comments

Comments
 (0)