Skip to content
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

Confirm processes are no longer visible before returning #37

Closed
wants to merge 5 commits into from

Conversation

Autre31415
Copy link

@Autre31415 Autre31415 commented Jan 18, 2020

This is a simple implementation of a confidence checker in response to my findings in #10.

Notes:

  • Added a verify option, which allows this feature to be toggled (true by default).
  • Added a verifyTimeout option, which allows the max time for checking to be configured in seconds (2 by default).
  • When updating index.d.ts I noticed the silent option was missing so I tossed that in as well.
  • A major goal of this design was to keep any refactoring of code around it to a minimum, though notably it could be made more optimal with heftier refactors (i.e, running it against every process at the same time after the fact).
  • The existence of this check deprecates the need for the noopProcessKilled function in tests along with its associated delay dev dependency.

@Autre31415 Autre31415 changed the title Confirm processes are no longer visible before returning WIP: Confirm processes are no longer visible before returning Jan 21, 2020
@sindresorhus
Copy link
Owner

Are you still interested in finishing this?

@Autre31415
Copy link
Author

Autre31415 commented Feb 23, 2020

I've merged in the latest and made some small tweaks. This is ready for review.

@Autre31415 Autre31415 changed the title WIP: Confirm processes are no longer visible before returning Confirm processes are no longer visible before returning Feb 23, 2020
index.js Outdated
let i = 0;

return new Promise(resolve => {
const checker = setInterval(async () => {
Copy link
Owner

Choose a reason for hiding this comment

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

It would be better to use setTimeout and call it recursively. Now, if processExists for some reason takes more than 100 ms, it will be called too often.

index.js Outdated
clearInterval(checker);
}

// Give up after 2 seconds
Copy link
Owner

Choose a reason for hiding this comment

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

Why 2 seconds? And the 2 seconds should be a constant, so it can easily be changed if needed.

index.js Outdated
const killChecker = async input => {
let i = 0;

return new Promise(resolve => {
Copy link
Owner

Choose a reason for hiding this comment

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

Almost anytime you want to use new Promise, there's a better way. You can just recursively await instead.

Copy link
Author

Choose a reason for hiding this comment

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

In this case new promise is being used to convert the callback nature of setInterval (would also apply in the setTimeout format) into a promise that we can then await. Could you expand a bit on how we could go about this conversion without using new promise?

Copy link
Owner

Choose a reason for hiding this comment

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

index.js Outdated
return new Promise(resolve => {
const checker = setInterval(async () => {
i++;
const exists = await processExists(input);
Copy link
Owner

Choose a reason for hiding this comment

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

If processExists rejects, it will cause an uncaught exception as it's not caught anywhere, since you're using new Promise.

@sindresorhus
Copy link
Owner

This behavior needs to be documented in the readme and index.d.ts.

return;
}

const seconds = options.verifyTimeout || 2;
Copy link
Owner

Choose a reason for hiding this comment

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

This will result in 2 if the user specifies 0 as timeout.

const exists = await processExists(input);

if (exists && checks > 0) {
timeout();
Copy link
Owner

Choose a reason for hiding this comment

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

What's the purpose of this? Since it's not awaited.

Copy link
Owner

Choose a reason for hiding this comment

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

Can you add a test that ensures it takes the time given in verifyTimeout? You can use https://github.com/sindresorhus/delay#delayrangeminimum-maximum-options for this, to give it a slight threshold.

Type: `boolean`\
Default: `true`

Perform a rigorous check that the process is no longer visible to the system before returning.
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
Perform a rigorous check that the process is no longer visible to the system before returning.
Perform a rigorous check that the process is no longer running before returning.

"visible" sounds weird. Makes me think it could be "hidden".


const fkillProxy = proxyquire('.', {'process-exists': processExistsStub});
await fkillProxy(pid, {verify: false, force: true});
t.true(count === 0);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
t.true(count === 0);
t.is(count, 0);

}

const seconds = options.verifyTimeout || 2;
let checks = seconds * 10;
Copy link
Owner

Choose a reason for hiding this comment

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

This logic is not sound. You cannot guarantee how long things take. It's better to just continue checking until you measure using Date() that 2 seconds have passed.


function timeout() {
return new Promise(resolve => setTimeout(resolve));
}
Copy link
Owner

Choose a reason for hiding this comment

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

This can be top-level and be an inline arrow function.


return checker();
};

const fkill = async (inputs, options = {}) => {
Copy link
Owner

Choose a reason for hiding this comment

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

Better to set defaults upfront.

Suggested change
const fkill = async (inputs, options = {}) => {
const fkill = async (inputs, {verify = true, veryifyTimeout = 2} = {}) => {

@sindresorhus
Copy link
Owner

@Autre31415 Bump

Base automatically changed from master to main January 23, 2021 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants