You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Serializer.SerializeResponseAsync writes synchronously to the target stream #399
I have verified that I am running the latest version of the SysML2.NET
I have searched open and closed issues to ensure it has not already been reported
Description
Both Serializer.SerializeResponseAsync overloads (IResponse and IEnumerable<IResponse>) perform a synchronous write on the target stream before their await writer.FlushAsync(...): the private WriteSingle and WriteMany helpers end with writer.Flush(), which calls Stream.Write.
On a stream that disallows synchronous I/O — ASP.NET Core's response body under Kestrel or TestServer, where AllowSynchronousIO is false by default — this throws:
System.InvalidOperationException: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true.
at System.IO.Stream.Write(ReadOnlySpan`1 buffer)
at System.Text.Json.Utf8JsonWriter.Flush()
at SysML2.NET.Serializer.Json.Serializer.WriteMany(...)
at SysML2.NET.Serializer.Json.Serializer.SerializeResponseAsync(IEnumerable`1 dataItems, ...)
Since the response status has usually been sent by then, an HTTP server answers 200 with an empty body. The SerializeAsync overloads (Core elements) are not affected: they use FlushAsync throughout.
Expected: the …Async methods perform no synchronous I/O on the target stream.
Suggested fix: remove writer.Flush() from WriteSingle / WriteMany and keep the trailing await writer.FlushAsync(cancellationToken) in the async callers; the synchronous SerializeResponse overloads then flush after calling the helpers.
Steps to Reproduce
Host a minimal ASP.NET Core endpoint that writes a PSM response straight to HttpResponse.Body:
Activity
Fix #399
Fix #399 (#401)