Repository navigation
fix!: reconnect to platform events websocket after connection drop - #778
Merged
Merged
Conversation
`PlatformEventManager` connected to the platform events websocket once and never reconnected after a drop, so a later `migrating` or `aborting` was missed for the rest of the run. The connection is now re-established with backoff (mirroring the Python SDK), except after a non-retryable close code. BREAKING CHANGE: `Actor.init()` now rejects when the first connection to the platform events websocket cannot be established, instead of only logging it. Closes #642
barjin
approved these changes
Oct 15, 2026
barjin
left a comment
Member
There was a problem hiding this comment.
Thank you @vladfrangu ,
one AI-assisted issue (probably worth fixing, given how simple the fix is), but the rest seems fine 👍
| /** Opens a new connection; resolves once it is established and rejects if it fails before that. */ | ||
| async #connect(eventsWsUrl: string) { | ||
| await new Promise<void>((resolve, reject) => { | ||
| const ws = new WebSocket(eventsWsUrl); |
Member
There was a problem hiding this comment.
ws apparently has no default handshake timeout. init() now waits for this, so on a silent server, Actor.init() now probably hangs forever.
Let's maybe just add this?
Suggested change
| const ws = new WebSocket(eventsWsUrl); | |
| const ws = new WebSocket(eventsWsUrl, { handshakeTimeout: 10_000 }); |
Member
Author
There was a problem hiding this comment.
Good catch, applied in a34272f with a comment explaining why. Also matches the Python websockets default open_timeout of 10 s.
[🤖] Claude Code using Fable 5.1
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.
PlatformEventManagernow reconnects to the platform events websocket after a drop, mirroring apify/apify-sdk-python#967. Previously a singlecloseended all platform events for the rest of the run, so a latermigrating/abortingwas missed and state was not persisted before migration.websocketsschedule: random 0–5 s, then 3.1 s × 1.618 up to 90 s, reset after a connection that stayed open at least 1 s. It applies to failed attempts and to rapid open-then-close loops alike.1002,1003,1007,1008and1010are treated as permanent: logged as an error, no reconnect.Actor.init()now rejects when the first connection cannot be established, instead of only logging the error. Documented in the v4 upgrading guide.Actor.init()registers theaborting/migratinghandlers before connecting. Sinceinit()now waits for the socket to open, the old order could miss an event sent right on connect (the existingabortingtest became flaky under load).close()cancels a pending backoff sleep or in-flight attempt, so shutdown never triggers a reconnect.Tests cover fail-fast init, reconnect after a
1000and a1011close, no reconnect on1008, bounded attempts under rapid closes, recovery after the server comes back, andclose()during a pending reconnect.Closes #642
[🤖] Claude Code using Fable 5.1