Skip to content

Commit

Permalink
Merge pull request #230 from joemphilips/cln_async_cancellation
Browse files Browse the repository at this point in the history
Cln async cancellation
  • Loading branch information
joemphilips authored Aug 25, 2022
2 parents 3f6dc67 + db48cbe commit f28237a
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/DotNetLightning.ClnRpc/Client.fs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,20 @@ type ClnClient
[<Optional; DefaultParameterValue(CancellationToken())>] ct: CancellationToken
) : Task<'T> =
backgroundTask {
use! networkStream = getTransportStream.Invoke(ct)
// without this hack, `ReadAsync` is blocking even if
// cancellation is requested.
use _ =
ct.Register(fun () ->
match networkStream with
| :? NetworkStream as s -> s.Socket.Dispose()
| _ -> ()

networkStream.Dispose()
)

match jsonLibrary with
| JsonLibraryType.Newtonsoft ->
use! networkStream = getTransportStream.Invoke(ct)

use textWriter =
new StreamWriter(networkStream, utf8, 1024 * 10, true)
Expand Down Expand Up @@ -310,7 +321,7 @@ type ClnClient
)
)

reqO.WriteToAsync(jsonWriter)
reqO.WriteToAsync(jsonWriter, ct)

do! jsonWriter.FlushAsync(ct)
do! textWriter.FlushAsync()
Expand Down Expand Up @@ -371,7 +382,7 @@ type ClnClient
return failwith "unreachable"

| JsonLibraryType.SystemTextJson ->
use! networkStream = getTransportStream.Invoke(ct)

use jsonWriter = new Utf8JsonWriter(networkStream)

// -- write --
Expand Down Expand Up @@ -401,7 +412,7 @@ type ClnClient
return Activator.CreateInstance returnType |> unbox
else
let buf = Array.zeroCreate(65535)
let length = networkStream.Read(buf.AsSpan())
let! length = networkStream.ReadAsync(buf.AsMemory(), ct)

if length = 0 then
return Activator.CreateInstance()
Expand Down

0 comments on commit f28237a

Please sign in to comment.