Skip to content

Fix proxy cleanup during rapid WebSocket reconnects#20

Merged
74th merged 1 commit intomainfrom
fix/rest-api
Mar 13, 2026
Merged

Fix proxy cleanup during rapid WebSocket reconnects#20
74th merged 1 commit intomainfrom
fix/rest-api

Conversation

@74th
Copy link
Owner

@74th 74th commented Mar 13, 2026

Summary

  • protect StackChan proxy registration with an async lock
  • replace the active proxy for an IP before closing the previous connection
  • only remove a proxy during cleanup when it is still the currently registered one
  • route REST API lookups through shared helpers that ignore closed proxies

Problem

When the firmware reconnects very quickly, the new WebSocket connection can be accepted before the previous connection finishes closing. Because the server tracks connections by client IP, the old connection's cleanup could remove the newly registered proxy. As a result, GET /v1/stackchan and related endpoints could incorrectly report that no StackChan was connected.

Fix

This change makes proxy registration and removal atomic and identity-aware:

  • _register_proxy() stores the new proxy under the client IP while holding a lock
  • the previous proxy is closed only after the replacement is already registered
  • _unregister_proxy() removes the mapping only if the proxy being cleaned up is still the active one for that IP

This preserves the latest connection across fast reconnects and keeps the REST API state consistent.

Testing

  • verified there are no diagnostics in stackchan_server/app.py

@74th 74th merged commit 086b48e into main Mar 13, 2026
1 check passed
@74th 74th deleted the fix/rest-api branch March 13, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant