-
Notifications
You must be signed in to change notification settings - Fork 31k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream: unref #48007
Open
rluvaton
wants to merge
11
commits into
nodejs:main
Choose a base branch
from
rluvaton:unref-stream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
stream: unref #48007
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
128c0b7
stream: unref wip
rluvaton e2f8efd
stream: unref wip
rluvaton 4e37e9d
stream: unref wip
rluvaton 2e83f52
stream: fix failed test and use wrap instead
rluvaton 16083e0
stream: unref fix missed 1 item in cache
rluvaton 5bc4b91
stream: use once when listening to error event
rluvaton b59b8dc
stream: add test for detecting memory leak
rluvaton 489cebf
stream: try to fix, this can have major consonances, really WIP
rluvaton 930b34c
stream: format
rluvaton e699f2a
Revert "stream: try to fix, this can have major consonances, really WIP"
rluvaton 9831b44
Merge branch 'main' into unref-stream
rluvaton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
'use strict'; | ||
require('../common'); | ||
|
||
const { | ||
Readable, | ||
pipeline, | ||
PassThrough | ||
} = require('stream'); | ||
const { it } = require('node:test'); | ||
const { strictEqual, deepStrictEqual } = require('assert'); | ||
|
||
const { from, unref } = Readable; | ||
|
||
const nextTick = () => new Promise((resolve) => process.nextTick(resolve)); | ||
|
||
it('unref stream error should propagate to original one', async () => { | ||
const originalStream = from([1, 2, 3, 4, 5]); | ||
|
||
let emittedError; | ||
originalStream.on('error', (e) => { | ||
emittedError = e; | ||
}); | ||
const unrefStream = unref(originalStream); | ||
|
||
const thrownError = new Error('test'); | ||
|
||
unrefStream.destroy(thrownError); | ||
|
||
await nextTick(); | ||
strictEqual(unrefStream.destroyed, true); | ||
strictEqual(originalStream.destroyed, true); | ||
|
||
// Need another tick to propagate the error | ||
await nextTick(); | ||
strictEqual(emittedError, thrownError); | ||
}); | ||
|
||
it('Original stream error should propagate to unref one', async () => { | ||
const originalStream = from([1, 2, 3, 4, 5]); | ||
const unrefStream = unref(originalStream); | ||
|
||
let emittedError; | ||
unrefStream.on('error', (e) => { | ||
emittedError = e; | ||
}); | ||
|
||
const thrownError = new Error('test'); | ||
|
||
originalStream.destroy(thrownError); | ||
|
||
await nextTick(); | ||
strictEqual(unrefStream.destroyed, true); | ||
strictEqual(originalStream.destroyed, true); | ||
|
||
await nextTick(); | ||
strictEqual(emittedError, thrownError); | ||
}); | ||
|
||
it('Should not close original stream when unref one finished but not consumed all data', async () => { | ||
const originalStream = from([1, 2, 3, 4, 5]); | ||
|
||
const unrefStream = unref(originalStream); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of unrefStream) { | ||
break; | ||
} | ||
|
||
await nextTick(); | ||
strictEqual(unrefStream.destroyed, true); | ||
strictEqual(originalStream.destroyed, false); | ||
}); | ||
|
||
it('Should continue consuming the original stream data from where the unref stopped', async () => { | ||
const originalStream = from([1, 2, 3, 4, 5]); | ||
|
||
const firstItem = await unref(originalStream).take(1).toArray(); | ||
deepStrictEqual(firstItem, [1]); | ||
|
||
const restOfData = await originalStream.toArray(); | ||
deepStrictEqual(restOfData, [2, 3, 4, 5]); | ||
}); | ||
|
||
it('Should close original stream when unref one consume all data', async () => { | ||
const originalStream = from([1, 2, 3, 4, 5]); | ||
|
||
const unrefStream = unref(originalStream); | ||
|
||
const data = await unrefStream.toArray(); | ||
deepStrictEqual(data, [1, 2, 3, 4, 5]); | ||
|
||
await nextTick(); | ||
|
||
strictEqual(unrefStream.destroyed, true); | ||
strictEqual(originalStream.destroyed, true); | ||
}); | ||
|
||
it('original stream close should close unref one', async () => { | ||
const originalStream = from([1, 2, 3, 4, 5]); | ||
const unrefStream = unref(originalStream); | ||
|
||
await originalStream.toArray(); | ||
|
||
strictEqual(originalStream.destroyed, true); | ||
strictEqual(unrefStream.destroyed, true); | ||
}); | ||
|
||
it('make sure not leaking memory', async () => { | ||
function getMemoryAllocatedInMB() { | ||
return Math.round(process.memoryUsage().rss / 1024 / 1024 * 100) / 100; | ||
} | ||
|
||
const bigData = () => from(async function* () { | ||
const obj = Array.from({ length: 100000 }, () => (Array.from({ length: 15 }, (_, i) => i))); | ||
while (true) { | ||
yield obj.map((item) => item.slice(0)); | ||
await new Promise((resolve) => setTimeout(resolve, 1)); | ||
} | ||
}()); | ||
|
||
const originalStream = pipeline(bigData(), new PassThrough({ objectMode: true }), () => { | ||
}); | ||
unref(originalStream); | ||
originalStream.iterator({ destroyOnReturn: true }); | ||
|
||
// Making sure some data passed so we won't catch something that is related to the infra | ||
const iterator = originalStream.iterator({ destroyOnReturn: true }); | ||
for (let j = 0; j < 10; j++) { | ||
await iterator.next(); | ||
} | ||
|
||
const currentMemory = getMemoryAllocatedInMB(); | ||
|
||
for (let j = 0; j < 10; j++) { | ||
await iterator.next(); | ||
} | ||
|
||
const newMemory = getMemoryAllocatedInMB(); | ||
|
||
originalStream.destroy(null); | ||
strictEqual(newMemory - currentMemory < 100, true, `After consuming 10 items the memory increased by ${Math.floor(newMemory - currentMemory)}MB`); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the following test show why 0 (it's part of the tests suite also)