Fix annoying negative timeout warnings. #1103
Open
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.
I went and asked copilot to fix these annoying warnings #1061 it implemented the suggested fix from the issue.
Here are its remarks:
Fixes the
TimeoutNegativeWarning
that occurs when the postgres library attempts to reconnect to the database after the intended reconnection time has already passed.Problem
The postgres library occasionally throws a
TimeoutNegativeWarning
error when attempting to reconnect:This occurs when the calculated delay value
closedDate + delay - performance.now()
becomes negative, meaning the intended reconnection time has already passed.Root Cause
In the
reconnect()
function, the current code calculates the timeout delay as:When
closedDate + delay - performance.now()
results in a negative value, Node.js emits aTimeoutNegativeWarning
and sets the timeout to 1ms.Solution
Wrapped the delay calculation in
Math.max()
to ensure the timeout value is never negative:This ensures:
Files Changed
src/connection.js
(line 353) - Main sourcecf/src/connection.js
(line 355) - Cloudflare versioncjs/src/connection.js
(line 353) - CommonJS versiondeno/src/connection.js
(line 356) - Deno versionAll transpiled versions are automatically updated via the build process to ensure consistency across environments.
Testing
Created comprehensive tests to verify:
TimeoutNegativeWarning
in negative timeout scenariosThis is a minimal, safe fix that preserves the existing reconnection logic while eliminating the Node.js warning.
Hope this helps