Skip to content

pubsub: stop creating negative timeouts - #9243

Open
smcgivern wants to merge 1 commit into
googleapis:mainfrom
smcgivern:patch-1
Open

pubsub: stop creating negative timeouts#9243
smcgivern wants to merge 1 commit into
googleapis:mainfrom
smcgivern:patch-1

Conversation

@smcgivern

Copy link
Copy Markdown

I didn't create an issue because it had so many required steps for a one-line fix, and those steps weren't applicable in this case.

This code assumes that latency is less than 90% of the deadline, but in our production environment we see that's not always the case. When it isn't, this library tries to create a timeout with negative duration, which throws a node TimeoutNegativeWarning.

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@smcgivern
smcgivern requested a review from a team as a code owner September 3, 2026 11:43
@product-auto-label product-auto-label Bot added the api: pubsub Issues related to the Pub/Sub API. label Sep 3, 2026
@github-actions
github-actions Bot requested a review from shivanee-p September 3, 2026 11:43

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

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.

Code Review

This pull request updates the lease manager to prevent negative timeouts by ensuring the calculated deadline extension is at least 0. The reviewer suggests enforcing a minimum delay of 1000ms instead of 0 to prevent a potential storm of rapid, back-to-back lease extension requests when latency is high.

const latency = this._subscriber.modAckLatency;

return (deadline * 0.9 - latency) * jitter;
return Math.max(0, deadline * 0.9 - latency) * jitter;

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

While using Math.max(0, ...) prevents negative timeouts and the resulting Node.js warnings, returning 0 when latency exceeds 90% of the deadline can lead to immediate rescheduling of lease extensions. Under high latency or network congestion, this can cause a storm of rapid, back-to-back modAck requests, further exacerbating the latency issue.

Consider enforcing a reasonable minimum delay (e.g., 1000ms) to prevent spamming the server when latency is high.

Suggested change
return Math.max(0, deadline * 0.9 - latency) * jitter;
return Math.max(1000, deadline * 0.9 - latency) * jitter;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Currently it's negative, so 🤷

@shivanee-p
shivanee-p removed their request for review September 3, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: pubsub Issues related to the Pub/Sub API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant