Diagnostics: surface upstream WS close reason + break the null-spin loop

The Pi journal showed 170 000 'upstream silent NxN' messages within one
second — i.e. ReceiveJsonAsync was returning null in zero time, so the
upstream WS to OpenAI was closed. The relay was spinning until idle
watchdog (30 s) fired. Add IRealtimeUpstream.IsClosed + CloseReason,
populated by OpenAIRealtimeUpstream from the close status / WS exception,
and have PumpAsync stop on close and forward
  error code=upstream_closed message=close status=<X> desc=<Y>
to the device. Closes the spin AND gives us the OpenAI close reason.
This commit is contained in:
2026-06-12 07:17:04 +00:00
parent 12be767325
commit 1a36c8096d
5 changed files with 50 additions and 8 deletions
+24 -4
View File
@@ -7,6 +7,10 @@ namespace backend.Realtime;
public class OpenAIRealtimeUpstream : IRealtimeUpstream
{
private readonly ClientWebSocket _ws = new();
private string? _closeReason;
public bool IsClosed => _ws.State != WebSocketState.Open && _ws.State != WebSocketState.Connecting;
public string? CloseReason => _closeReason;
public async Task ConnectAsync(string apiKey, string model, CancellationToken ct)
{
@@ -31,11 +35,27 @@ public class OpenAIRealtimeUpstream : IRealtimeUpstream
while (true)
{
WebSocketReceiveResult result;
try { result = await _ws.ReceiveAsync(buffer, ct); }
catch (WebSocketException) { return null; }
catch (OperationCanceledException) { return null; }
try
{
result = await _ws.ReceiveAsync(buffer, ct);
}
catch (WebSocketException exc)
{
_closeReason ??= $"WebSocketException:{exc.WebSocketErrorCode}:{exc.Message}";
return null;
}
catch (OperationCanceledException)
{
return null;
}
if (result.MessageType == WebSocketMessageType.Close) return null;
if (result.MessageType == WebSocketMessageType.Close)
{
var status = _ws.CloseStatus?.ToString() ?? "<none>";
var desc = _ws.CloseStatusDescription ?? "<no description>";
_closeReason ??= $"close status={status} desc={desc}";
return null;
}
stream.Write(buffer, 0, result.Count);
if (result.EndOfMessage)
{