Skip to content

fix: Implement robust watchdog for stream query timeouts#281

Merged
JS-AK merged 1 commit into
masterfrom
fix/pg-core/streams
Oct 15, 2025
Merged

fix: Implement robust watchdog for stream query timeouts#281
JS-AK merged 1 commit into
masterfrom
fix/pg-core/streams

Conversation

@JS-AK
Copy link
Copy Markdown
Owner

@JS-AK JS-AK commented Oct 15, 2025

This commit resolves a critical issue where streaming queries would crash when a client-side query_timeout was configured. The root cause is a known bug in node-postgres (issue #1860) that mishandles timeouts for streamable query objects that lack a callback property.

The fix introduces a client-side "time-to-first-chunk" watchdog that replaces the problematic built-in timeout mechanism for streams:

  1. Watchdog for First Chunk: A timer is now set based on the client.query_timeout or connectionParameters.query_timeout. This timer is cleared as soon as the first chunk of data is received, ensuring that long-running streams that are actively sending data do not time out.

  2. Clear Error Messaging: If a timeout occurs, the stream is destroyed with a detailed error message that correctly identifies whether the timeout was triggered by a client-level or connection-level setting.

  3. Refactoring: The watchdog logic has been extracted into a reusable setupStreamWatchdog helper function to improve code clarity and remove duplication.

  4. Comprehensive Testing: Added a suite of tests for stream timeouts, including positive cases, and negative cases for both pooled and direct client connections to ensure the watchdog behaves as expected.

This commit resolves a critical issue where streaming queries would crash when a client-side `query_timeout` was configured. The root cause is a known bug in `node-postgres` (issue #1860) that mishandles timeouts for streamable query objects that lack a callback property.

The fix introduces a client-side "time-to-first-chunk" watchdog that replaces the problematic built-in timeout mechanism for streams:

1.  **Watchdog for First Chunk**: A timer is now set based on the `client.query_timeout` or `connectionParameters.query_timeout`. This timer is cleared as soon as the first chunk of data is received, ensuring that long-running streams that are actively sending data do not time out.

2.  **Clear Error Messaging**: If a timeout occurs, the stream is destroyed with a detailed error message that correctly identifies whether the timeout was triggered by a client-level or connection-level setting.

3.  **Refactoring**: The watchdog logic has been extracted into a reusable `setupStreamWatchdog` helper function to improve code clarity and remove duplication.

4.  **Comprehensive Testing**: Added a suite of tests for stream timeouts, including positive cases, and negative cases for both pooled and direct client connections to ensure the watchdog behaves as expected.
@JS-AK JS-AK merged commit e055882 into master Oct 15, 2025
1 check passed
@JS-AK
Copy link
Copy Markdown
Owner Author

JS-AK commented Oct 15, 2025

🎉 This PR is included in version 3.48.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@JS-AK JS-AK added the released label Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant