fix: protect Eio's out channel with a mutex
Some checks failed
github pages / deploy (push) Has been cancelled
build / build4 (4.14.x, ubuntu-latest) (push) Has been cancelled
build / build5 (5.1.x, ubuntu-latest) (push) Has been cancelled
build / build5 (5.2.x, ubuntu-latest) (push) Has been cancelled
build / build5 (5.3.x, ubuntu-latest) (push) Has been cancelled

close #58
This commit is contained in:
Simon Cruanes 2025-11-25 19:43:50 -05:00
parent 00479b0f05
commit fb49472f34
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 16 additions and 7 deletions

View file

@ -1,4 +1,9 @@
# 0.11
- breaking: the Eio library now needs the output channel to be paired with
an `Eio.Mutex.t` to prevent race conditions (#58)
# 0.10 # 0.10
- use `git subtree` to vendor lsp+jsonrpc, so that they - use `git subtree` to vendor lsp+jsonrpc, so that they

View file

@ -9,7 +9,7 @@ module IO_eio :
with type 'a t = 'a with type 'a t = 'a
and type env = Eio_unix.Stdenv.base and type env = Eio_unix.Stdenv.base
and type in_channel = Eio.Buf_read.t and type in_channel = Eio.Buf_read.t
and type out_channel = Eio_unix.sink_ty Eio.Std.r = struct and type out_channel = Eio.Mutex.t * Eio_unix.sink_ty Eio.Std.r = struct
type 'a t = 'a type 'a t = 'a
let ( let+ ) x f = f x let ( let+ ) x f = f x
@ -28,17 +28,21 @@ module IO_eio :
let stdin env = let stdin env =
Eio.Buf_read.of_flow ~max_size:1_000_000 (Eio.Stdenv.stdin env) Eio.Buf_read.of_flow ~max_size:1_000_000 (Eio.Stdenv.stdin env)
let stdout = Eio.Stdenv.stdout let stdout_mutex = Eio.Mutex.create ()
let stdout env = stdout_mutex, Eio.Stdenv.stdout env
type env = Eio_unix.Stdenv.base type env = Eio_unix.Stdenv.base
type in_channel = Eio.Buf_read.t type in_channel = Eio.Buf_read.t
type out_channel = Eio_unix.sink_ty Eio.Std.r type out_channel = Eio.Mutex.t * Eio_unix.sink_ty Eio.Std.r
let write_string out_ch str = Eio.Flow.copy_string str out_ch let write_string (mutex, out_ch) str =
Eio.Mutex.use_rw ~protect:false mutex (fun () ->
Eio.Flow.copy_string str out_ch)
let write out_ch bytes off len = let write (mutex, out_ch) bytes off len =
Eio.Mutex.use_rw ~protect:false mutex (fun () ->
Eio.Buf_write.with_flow out_ch @@ fun w -> Eio.Buf_write.with_flow out_ch @@ fun w ->
Eio.Buf_write.bytes w ~off ~len bytes Eio.Buf_write.bytes w ~off ~len bytes)
let read in_ch bytes off len = let read in_ch bytes off len =
let str = Eio.Buf_read.take len in_ch in let str = Eio.Buf_read.take len in_ch in