cloudflare / cloudflare/containers
WebSocket connections don't renew activity timeout - container sleeps despite active connections
- Dominant language
- TypeScript
- Stars
- 270
- Forks
- 42
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 4
Description
## Summary
The README states that WebSocket connections automatically renew the activity timeout:
> The Container also automatically renews the activity timeout when WebSocket messages are sent or received.
However, looking at the implementation, this doesn't appear to be the case. The container will sleep after `sleepAfter` expires, even with active WebSocket connections.
## Analysis
`renewActivityTimeout()` is only called:
- Once in the constructor
- Once at the start of `containerFetch()` (before the WebSocket upgrade)
- During `startContainerIfNotRunning()`
- After `onActivityExpired()` to prevent spam
After the WebSocket upgrade completes, `tcpPort.fetch()` proxies frames directly between client and container. The DO's JavaScript code never runs for individual WebSocket messages, so there's no opportunity to call `renewActivityTimeout()`.
## Reproduction
1. Create a container with `sleepAfter = '1m'`
2. Connect via WebSocket
3. Keep sending/receiving WebSocket messages
4. After 1 minute, the container is stopped despite the active WebSocket connection
## Expected Behavior
Active WebSocket connections should keep the container alive, either by:
1. `tcpPort` notifying the DO of WebSocket frame activity
2. Tracking active WebSocket connections and not expiring while any exist
3. Some other mechanism to detect ongoing WebSocket traffic
## Workaround
Currently the only workaround is to send periodic HTTP requests to manually refresh the activity timeout, which somewhat defeats the purpose of using WebSockets.
Contributor guide
Assessment
This issue has not been assessed yet.