@@ -86,16 +86,11 @@ public override async Task OnConnectedAsync()
8686 var currentSnapshot = _metricsCollector . LatestSnapshot ;
8787 await Clients . Caller . ReceiveMetrics ( currentSnapshot ) ;
8888
89- // Send current idle state to the connecting client
90- var idleData = new IdleStateData
91- {
92- IsIdle = _idleStateService . IsIdle ,
93- Message = _idleStateService . IsIdle
94- ? "Application is idle, no health probes being sent. There will be gaps in diagnostics and logs."
95- : "Application is active" ,
96- Timestamp = DateTimeOffset . UtcNow
97- } ;
98- await Clients . Caller . ReceiveIdleState ( idleData ) ;
89+ // NOTE: We intentionally do NOT send idle state here.
90+ // The client calls WakeUp() immediately after connecting, which will
91+ // wake the app if needed and send the correct idle state.
92+ // Sending IsIdle=true here would cause the client to disconnect
93+ // before WakeUp() can be invoked (race condition).
9994
10095 await base . OnConnectedAsync ( ) ;
10196 }
@@ -142,17 +137,18 @@ public async Task WakeUp()
142137 if ( wasIdle )
143138 {
144139 _logger . LogInformation ( "Server woken up by client request from: {ConnectionId}" , Context . ConnectionId ) ;
145- // The MetricsBroadcastService will broadcast the wake-up message to all clients
146- // via the WakingUp event, so we don't need to send it directly here
147- return ;
148140 }
149141
150- // Only send direct response when app was already active (not waking up)
151- // to confirm current state to caller
142+ // Always send current idle state directly to the caller.
143+ // When waking from idle, the broadcast via MetricsBroadcastService may be
144+ // delayed (queued on the dedicated broadcast thread), so we must send
145+ // the updated state directly to ensure the client knows we're active.
152146 var idleData = new IdleStateData
153147 {
154- IsIdle = _idleStateService . IsIdle ,
155- Message = "Application is active" ,
148+ IsIdle = false ,
149+ Message = wasIdle
150+ ? "App waking up from idle state. There may be gaps in diagnostics and logs."
151+ : "Application is active" ,
156152 Timestamp = DateTimeOffset . UtcNow
157153 } ;
158154 await Clients . Caller . ReceiveIdleState ( idleData ) ;
0 commit comments