Skip to content

feat(data-connect): add automatic disconnect and cleanup logic to streaming, harden error handling#9808

Merged
stephenarosaj merged 13 commits intopasta/mainfrom
pasta/rotini
Apr 6, 2026
Merged

feat(data-connect): add automatic disconnect and cleanup logic to streaming, harden error handling#9808
stephenarosaj merged 13 commits intopasta/mainfrom
pasta/rotini

Conversation

@stephenarosaj
Copy link
Copy Markdown
Contributor

@stephenarosaj stephenarosaj commented Apr 3, 2026

Description

✨ This PR improves error handling and implements robust cleanup logic for the streaming transport during disconnects and authentication changes.

Changes

  • Automatic Disconnects: Automatically disconnect the stream after 60 seconds of no active subscriptions. Wait to close until pending execute requests finish.
  • Stream Cleanup: Added state cleanup immediately upon disconnect.
  • Error Handling: Enhanced error generation and logging for stream operations.
  • Auth Changes: Enforced stream closure on illegal authentication state changes. Every auth state change except for a same-user token refresh will tear down the stream.
  • Query Layer: Added subscription cleanup when the stream disconnects externally.

Testing

  • Unit tests were added to websocketTransport.test.ts and related files to verify disconnect, cleanup, and error behavior.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 3, 2026

⚠️ No Changeset found

Latest commit: 0acc814

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements automatic stream disconnection for idle connections and handles authentication state changes. It introduces a 60-second idle timeout that triggers when no active subscriptions remain, and logic to immediately disconnect and reject pending requests if a user logs in, logs out, or changes identity. Review feedback highlights a potential null pointer exception when retrieving the user ID and suggests replacing the hardcoded timeout value with a named constant for better maintainability.

@stephenarosaj stephenarosaj changed the title feat(data-connect): add disconnect and cleanup logic to streaming feat(data-connect): add disconnect and cleanup logic to streaming, harden error handling Apr 5, 2026
@stephenarosaj stephenarosaj marked this pull request as ready for review April 6, 2026 07:45
@stephenarosaj stephenarosaj requested review from a team and dconeybe as code owners April 6, 2026 07:45
@stephenarosaj
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces idle connection management and improved error handling for the Data Connect stream transport. Key changes include a 60-second idle timeout that closes the connection when no subscriptions are active, immediate disconnection upon authentication changes (login, logout, or user change), and enhanced error message extraction in both REST and WebSocket layers. Additionally, the QueryManager now cleans up local subscriptions when an external WebSocket disconnection occurs. Review feedback focuses on refining the authentication change logic to prevent unnecessary resets during initial state resolution, removing redundant code in the connection closure process, centralizing hardcoded error strings, and ensuring idle timers are cleared when connections are closed manually.

e &&
typeof e === 'object' &&
'message' in e &&
e.message === 'WebSocket disconnected externally'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error message string "WebSocket disconnected externally" is hardcoded here and in the transport layer (e.g., in websocket.ts). This creates a maintenance risk where changing the message in one place will break the cleanup logic in the other. Consider using a shared constant for this error message to ensure consistency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - error hardening will come in a future PR.

@stephenarosaj stephenarosaj changed the title feat(data-connect): add disconnect and cleanup logic to streaming, harden error handling feat(data-connect): add automatic disconnect and cleanup logic to streaming, harden error handling Apr 6, 2026
@stephenarosaj stephenarosaj merged commit c9a993e into pasta/main Apr 6, 2026
29 checks passed
@stephenarosaj stephenarosaj deleted the pasta/rotini branch April 6, 2026 22:38
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.

2 participants