fix(websocket): stop LogCallbackEvent broadcast recursion in shutdown - #101
Merged
Merged
Conversation
EventBroadcaster.Initialize subscribed to LogCallbackEvent with
`e => Broadcast("LogCallback", e)`. Broadcast's catch block called
Log.Warning(...) on failure. On 7DTD, Log.Warning fires another
LogCallbackEvent through the same bus -> re-enters this subscriber
-> Broadcast throws again -> Log.Warning again -> infinite recursion
-> stack overflow -> KC crashes.
The throw is reliably reproducible during shutdown: once the
WebSocketServer manager has stopped, _server.WebSocketServices...
.Broadcast(json) raises "The current state of the manager is not
Start." Observed live on a Windows prod box -- 65+ minutes after a
botched auto-start, KC's GracefulRestart wedge cascade ended in
"Error in event handler for LogCallbackEvent: The requested operation
caused a stack overflow." repeated dozens of times in the log,
crashing KC and forcing a service auto-restart.
Fix, scoped narrowly to the LogCallbackEvent path:
1. [ThreadStatic] _inLogBroadcast re-entrancy guard. Only on the
LogCallback path -- other event types don't loop back into the
logger from their failure handler, so they keep the existing
Log.Warning-on-failure to surface real broadcast bugs.
2. Pulled the LogCallbackEvent subscriber out into a named
BroadcastLogCallback method so the guard logic stays readable
instead of being inlined in a lambda. Sets the flag, calls
Broadcast, clears the flag in finally.
3. Broadcast<T> gains a suppressFailureLogging parameter. When true
(LogCallbackEvent path), the catch falls back to
Console.Error.WriteLine instead of Log.Warning so the failure
notification itself cannot fire a fresh LogCallbackEvent and
restart the recursion. Wrapped in its own try/catch in case
stderr is also unhappy during shutdown.
The dropped log-broadcast on re-entry is the right tradeoff: we'd
only be re-entering because Broadcast just failed, which means the
ws clients aren't going to see this log line anyway. Better one lost
diagnostic line than a crashed service.
No unit test included. EventBroadcaster is a static class tightly
coupled to WebSocketSharp.Server and the 7DTD Log type (already
called out as game-runtime-only in ModEventBusTests.cs:97). The
actual recursion only happens when _server is non-null AND Broadcast
throws, neither of which is reachable from the existing test harness
without restructuring the class. The fix is small and the failure
mode is reproducible in vivo -- verifying the absence of recursion
in prod logs after deploy is the better signal.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
5 tasks
AdaInTheLab
added a commit
that referenced
this pull request
Jun 4, 2026
Cuts v2.8.2. Three fixes from the live Windows prod box plus the timezone field on the Server Restart panel grows up into a host- resolvable dropdown. No new pages, no schema changes — patch release per docs/RELEASES.md conventions. What lands: - #101 fix(websocket): stop LogCallbackEvent broadcast recursion in shutdown. ThreadStatic re-entrancy guard + suppressFailureLogging fallback to Console.Error so a failed log-broadcast can't fire a fresh LogCallbackEvent and recurse to stack overflow. - #102 fix(restart): skip systemctl probe on Windows; route through OS-aware strategy. New Core/OsRestartStrategy.cs picks per OS; Windows goes straight to in-game shutdown + NSSM AppExit Restart bounce, no more wasted 5s probe or misleading warning. - #103 feat(restart): host-resolvable timezone dropdown + heal-on-read. LoadPersistedSettings heals an unresolvable TZ ID to TimeZoneInfo .Local and persists. New GET /api/server/timezones endpoint feeds a PrimeVue Select in Settings → Server Restart, replacing the free- text input that admins kept filling with strings the host couldn't parse. Version bumps: - src/KitsuneCommand/ModInfo.xml: 2.8.1 → 2.8.2 - frontend/package.json: 2.7.4 → 2.8.2 (reconciles prior drift — frontend version was stuck at 2.7.4 since v2.8.0) - CHANGELOG.md: promote [Unreleased] → [2.8.2] - 2026-06-04 Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EventBroadcaster.Initializesubscribed toLogCallbackEventwithe => Broadcast("LogCallback", e).Broadcast's catch block calledLog.Warning(...)on failure. On 7DTD,Log.Warningfires anotherLogCallbackEventthrough the same bus → re-enters this subscriber →Broadcastthrows again →Log.Warningagain → infinite recursion → stack overflow → KC crashes.The throw is reliably reproducible during shutdown: once the
WebSocketServermanager has stopped,_server.WebSocketServices...Broadcast(json)raises"The current state of the manager is not Start."Observed live on a Windows prod box — 65+ minutes after a botched auto-start, KC'sGracefulRestartwedge cascade ended in"Error in event handler for LogCallbackEvent: The requested operation caused a stack overflow."repeated dozens of times in the log, crashing KC and forcing a service auto-restart.Fix, scoped narrowly to the LogCallbackEvent path
[ThreadStatic] _inLogBroadcastre-entrancy guard. Only on theLogCallbackpath — other event types don't loop back into the logger from their failure handler, so they keep the existingLog.Warning-on-failure to surface real broadcast bugs.Pulled the
LogCallbackEventsubscriber out into a namedBroadcastLogCallbackmethod so the guard logic stays readable instead of being inlined in a lambda. Sets the flag, callsBroadcast, clears the flag infinally.Broadcast<T>gains asuppressFailureLoggingparameter. When true (LogCallbackEvent path), the catch falls back toConsole.Error.WriteLineinstead ofLog.Warningso the failure notification itself cannot fire a freshLogCallbackEventand restart the recursion. Wrapped in its own try/catch in case stderr is also unhappy during shutdown.The dropped log-broadcast on re-entry is the right tradeoff: we'd only be re-entering because
Broadcastjust failed, which means the ws clients aren't going to see this log line anyway. Better one lost diagnostic line than a crashed service.On testing
No unit test included.
EventBroadcasteris a static class tightly coupled toWebSocketSharp.Serverand the 7DTDLogtype (already called out as game-runtime-only inModEventBusTests.cs:97). The actual recursion only happens when_serveris non-null ANDBroadcastthrows, neither of which is reachable from the existing test harness without restructuring the class. The fix is small and the failure mode is reproducible in vivo — verifying the absence of recursion in prod logs after deploy is the better signal.Test plan
GracefulRestartand confirm no "stack overflow" lines in log🤖 Generated with Claude Code