Skip to content

events: resume emitter when async iterator closes while paused - #67942

Open
bunnysayzz wants to merge 1 commit into
nodejs:mainfrom
bunnysayzz:fix/events-on-resume-after-return-67917
Open

bunnysayzz wants to merge 1 commit into
nodejs:mainfrom
bunnysayzz:fix/events-on-resume-after-return-67917

Conversation

@bunnysayzz

Copy link
Copy Markdown
Contributor

Fixes: #67917

events.on() with a low highWatermark pauses the emitter once the buffer fills, but closing the iterator with return() never resumed it. closeHandler() cleared the local paused flag without calling emitter.resume(), and the only resume path lived in next(), which never runs again after the iterator is closed. The stream stayed paused forever.

This resumes the emitter in closeHandler() when it was paused by the watermark controller, and adds a regression test asserting isPaused() is false after return() with a filled buffer.

I verified the bug with a standalone repro on Node v24.22.1 (isPaused() stayed true after return()) and traced the root cause in lib/events.js. I could not build Node from source here, so the new test has not been run locally yet, CI should confirm it.

AI assistance disclosure: I used an AI coding assistant to help write the fix and the test, and I verified the root cause and the change myself.

When the watermark controller pauses the emitter and the iterator
is closed via return() before the buffer drains, closeHandler()
cleared the paused flag without resuming the emitter, so the
stream stayed paused forever. Resume it when closing while paused.

Fixes: nodejs#67917
Signed-off-by: bunnysayzz <stfuazzo@gmail.com>
@nodejs-github-bot nodejs-github-bot added events Issues and PRs related to EventEmitter and the events module. needs-ci PRs that need a full CI run. labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

@MikeMcC399

Copy link
Copy Markdown
Member

I could not build Node from source here, so the new test has not been run locally yet, CI should confirm it.

This PR is failing CI tests.

See Pull requests > Step 6: Test

Before submitting your changes in a pull request, always run the full Node.js test suite.

with further details under BUILDING > Running tests.

Please make sure that you have run tests locally and corrected any problems before pushing any further changes to your PR.

@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.57%. Comparing base (408fdc5) to head (4ed7b38).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67942   +/-   ##
=======================================
  Coverage   90.57%   90.57%           
=======================================
  Files         802      802           
  Lines      286674   286677    +3     
  Branches    55378    55384    +6     
=======================================
+ Hits       259655   259665   +10     
+ Misses      17125    17112   -13     
- Partials     9894     9900    +6     
Files with missing lines Coverage Δ
lib/events.js 99.60% <100.00%> (+<0.01%) ⬆️

... and 25 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread lib/events.js
paused = false;
if (paused) {
paused = false;
emitter.resume();

@dcavalcante dcavalcante Dec 1, 2026 •

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.

This resumes on every close path, including when the emitter closes on its own. readline.Interface.resume() throws after close, so this crashes when the input ends.. that's the test-readline-async-iterators failure in CI here.

It only needs to resume when the consumer stops early (break or return()), not when the emitter ends. Moving it into return() with a !finished guard avoids resuming an already-closed emitter:

return() {
  if (paused && !finished) {
    paused = false;
    emitter.resume();
  }
  return closeHandler();
},

With that, closeHandler can stay as the original paused = false;. I tested this against test-readline-async-iterators and it passes..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:automated-account events Issues and PRs related to EventEmitter and the events module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

events: on() leaves emitter paused after return()

4 participants