forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: force-kill the captured PET child so restart cannot lose or misfire the kill #19
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
Open
StellaHuang95
wants to merge
2
commits into
main
Choose a base branch
from
fix-pet-force-kill
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
Changes from all commits
Commits
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
164 changes: 164 additions & 0 deletions
164
src/test/managers/common/nativePythonFinder.killProcess.unit.test.ts
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,164 @@ | ||
| import assert from 'node:assert'; | ||
| import * as sinon from 'sinon'; | ||
| import { killPetProcessWithGrace } from '../../../managers/common/nativePythonFinder'; | ||
|
|
||
| suite('killPetProcessWithGrace (PET force-kill ownership)', () => { | ||
| const GRACE_MS = 500; | ||
|
|
||
| let clock: sinon.SinonFakeTimers; | ||
| let outputChannel: { info: sinon.SinonStub; error: sinon.SinonStub }; | ||
|
|
||
| setup(() => { | ||
| clock = sinon.useFakeTimers(); | ||
| outputChannel = { info: sinon.stub(), error: sinon.stub() }; | ||
| }); | ||
|
|
||
| teardown(() => { | ||
| clock.restore(); | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| function makeProc( | ||
| exitCode: number | null = null, | ||
| signalCode: NodeJS.Signals | null = null, | ||
| ): { exitCode: number | null; signalCode: NodeJS.Signals | null; kill: sinon.SinonStub } { | ||
| return { exitCode, signalCode, kill: sinon.stub().returns(true) }; | ||
| } | ||
|
|
||
| test('sends SIGTERM to the running child and relinquishes ownership synchronously', () => { | ||
| const original = makeProc(null); | ||
| let holder: typeof original | undefined = original; | ||
|
|
||
| killPetProcessWithGrace( | ||
| () => holder, | ||
| () => { | ||
| holder = undefined; | ||
| }, | ||
| outputChannel, | ||
| ); | ||
|
|
||
| assert.strictEqual(holder, undefined, 'ownership should be relinquished before any async work'); | ||
| assert.ok(original.kill.calledOnceWithExactly('SIGTERM'), 'original should receive exactly one SIGTERM'); | ||
| assert.ok(outputChannel.info.called, 'kill should be logged'); | ||
| }); | ||
|
|
||
| test('escalates to SIGKILL on the captured child after the grace period', () => { | ||
| const original = makeProc(null); | ||
| let holder: typeof original | undefined = original; | ||
|
|
||
| killPetProcessWithGrace( | ||
| () => holder, | ||
| () => { | ||
| holder = undefined; | ||
| }, | ||
| outputChannel, | ||
| ); | ||
|
|
||
| assert.ok(original.kill.calledWith('SIGTERM'), 'SIGTERM sent immediately'); | ||
| assert.ok(!original.kill.calledWith('SIGKILL'), 'SIGKILL not sent before grace elapses'); | ||
|
|
||
| clock.tick(GRACE_MS); | ||
|
|
||
| assert.ok(original.kill.calledWith('SIGKILL'), 'SIGKILL sent after grace period'); | ||
| assert.strictEqual(original.kill.callCount, 2, 'exactly SIGTERM then SIGKILL'); | ||
| }); | ||
|
|
||
| test('does not force-kill a child that exits during the grace period', () => { | ||
| const original = makeProc(null); | ||
| let holder: typeof original | undefined = original; | ||
|
|
||
| killPetProcessWithGrace( | ||
| () => holder, | ||
| () => { | ||
| holder = undefined; | ||
| }, | ||
| outputChannel, | ||
| ); | ||
|
|
||
| original.exitCode = 0; | ||
| clock.tick(GRACE_MS); | ||
|
|
||
| assert.ok(original.kill.calledOnceWithExactly('SIGTERM'), 'only SIGTERM, no SIGKILL for an exited child'); | ||
|
StellaHuang95 marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| test('does not force-kill a child terminated by SIGTERM during the grace period', () => { | ||
| const original = makeProc(null); | ||
| let holder: typeof original | undefined = original; | ||
|
|
||
| killPetProcessWithGrace( | ||
| () => holder, | ||
| () => { | ||
| holder = undefined; | ||
| }, | ||
| outputChannel, | ||
| ); | ||
|
|
||
| original.signalCode = 'SIGTERM'; | ||
| clock.tick(GRACE_MS); | ||
|
|
||
| assert.ok( | ||
| original.kill.calledOnceWithExactly('SIGTERM'), | ||
| 'only SIGTERM, no SIGKILL for a signal-terminated child', | ||
| ); | ||
| }); | ||
|
|
||
| test('does not signal a child that had already exited, but still clears ownership', () => { | ||
| const original = makeProc(143); | ||
| let holder: typeof original | undefined = original; | ||
|
|
||
| killPetProcessWithGrace( | ||
| () => holder, | ||
| () => { | ||
| holder = undefined; | ||
| }, | ||
| outputChannel, | ||
| ); | ||
| clock.tick(GRACE_MS); | ||
|
|
||
| assert.strictEqual(holder, undefined, 'ownership cleared even when no signal is needed'); | ||
| assert.strictEqual(original.kill.callCount, 0, 'no signals sent to an already-exited child'); | ||
| assert.ok(outputChannel.info.notCalled, 'no kill message logged when nothing is killed'); | ||
| }); | ||
|
|
||
| test('never kills a replacement child assigned during the grace period', () => { | ||
| const original = makeProc(null); | ||
| const replacement = makeProc(null); | ||
| let holder: typeof original | undefined = original; | ||
|
|
||
| killPetProcessWithGrace( | ||
| () => holder, | ||
| () => { | ||
| holder = undefined; | ||
| }, | ||
| outputChannel, | ||
| ); | ||
|
|
||
| holder = replacement; | ||
| clock.tick(GRACE_MS); | ||
|
|
||
| assert.ok(original.kill.calledWith('SIGKILL'), 'the captured original is force-killed'); | ||
| assert.strictEqual(replacement.kill.callCount, 0, 'the replacement child is never signalled'); | ||
| }); | ||
|
|
||
| test('catches and logs SIGTERM errors without throwing, and still clears ownership', () => { | ||
| const original = makeProc(null); | ||
| original.kill = sinon.stub().throws(new Error('kill failed')); | ||
| let holder: typeof original | undefined = original; | ||
|
|
||
| assert.doesNotThrow(() => | ||
| killPetProcessWithGrace( | ||
| () => holder, | ||
| () => { | ||
| holder = undefined; | ||
| }, | ||
| outputChannel, | ||
| ), | ||
| ); | ||
|
|
||
| assert.strictEqual(holder, undefined, 'ownership relinquished even when the kill throws'); | ||
| assert.ok(outputChannel.error.called, 'kill error is logged'); | ||
|
|
||
| clock.tick(GRACE_MS); | ||
| assert.strictEqual(original.kill.callCount, 1, 'no SIGKILL scheduled after a SIGTERM failure'); | ||
| }); | ||
| }); | ||
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.