-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(core): Avoid blocking the process when calling flush on empty buffer
#19062
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f40eb35
Initial plan
Copilot 1438d5b
Fix client.flush() blocking process exit with unref'd timers and earl…
Copilot f9f3f6b
Fix lint error in client.test.ts
Copilot dfa682b
deslop
Lms24 92aa121
extract safeUnref
Lms24 8746622
size check
Lms24 4d8a973
stop flushing too early
Lms24 39baa2f
Update packages/core/src/utils/timer.ts
Lms24 917fc69
make test more strict
Lms24 35ace69
do not unref the 1ms delay
Lms24 755abb3
use safeUnref in offline transport
Lms24 d93d93c
lint
Lms24 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
| /** | ||
| * Calls `unref` on a timer, if the method is available on @param timer. | ||
| * | ||
| * `unref()` is used to allow processes to exit immediately, even if the timer | ||
| * is still running and hasn't resolved yet. | ||
| * | ||
| * Use this in places where code can run on browser or server, since browsers | ||
| * do not support `unref`. | ||
| */ | ||
| export function safeUnref(timer: ReturnType<typeof setTimeout>): ReturnType<typeof setTimeout> { | ||
| if (typeof timer === 'object' && typeof timer.unref === 'function') { | ||
| timer.unref(); | ||
| } | ||
| return timer; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { safeUnref } from '../../../src/utils/timer'; | ||
|
|
||
| describe('safeUnref', () => { | ||
| it('calls unref on a NodeJS timer', () => { | ||
| const timeout = setTimeout(() => {}, 1000); | ||
| const unrefSpy = vi.spyOn(timeout, 'unref'); | ||
| safeUnref(timeout); | ||
| expect(unrefSpy).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('returns the timer', () => { | ||
| const timeout = setTimeout(() => {}, 1000); | ||
| const result = safeUnref(timeout); | ||
| expect(result).toBe(timeout); | ||
| }); | ||
|
|
||
| it('handles multiple unref calls', () => { | ||
| const timeout = setTimeout(() => {}, 1000); | ||
| const unrefSpy = vi.spyOn(timeout, 'unref'); | ||
|
|
||
| const result = safeUnref(timeout); | ||
| result.unref(); | ||
|
|
||
| expect(unrefSpy).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("doesn't throw for a browser timer", () => { | ||
| const timer = safeUnref(385 as unknown as ReturnType<typeof setTimeout>); | ||
| expect(timer).toBe(385); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.