-
Notifications
You must be signed in to change notification settings - Fork 534
Require positive timeouts for synchronous child processes #52978
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,7 +109,7 @@ function resolveSyncExecMethod(node: TSESTree.CallExpression, sourceCode: TSESLi | |
| return null; | ||
| } | ||
|
|
||
| /** Returns true when the options-object argument for the call statically carries a non-nullish `timeout` property. */ | ||
| /** Returns true when the options-object argument for the call statically carries a positive `timeout` property. */ | ||
|
|
||
| function hasTimeoutOption(node: TSESTree.CallExpression, method: SyncExecMethod): boolean { | ||
| const optionsArg = getOptionsArgument(node, method); | ||
| if (!optionsArg) return false; | ||
|
|
@@ -127,8 +127,11 @@ function hasTimeoutOption(node: TSESTree.CallExpression, method: SyncExecMethod) | |
| if (!isTimeoutProp) continue; | ||
|
|
||
| const value = prop.value; | ||
| const isNullish = (value.type === AST_NODE_TYPES.Literal && value.value == null) || (value.type === AST_NODE_TYPES.Identifier && value.name === "undefined"); | ||
| if (!isNullish) return true; | ||
| const isMissingTimeout = | ||
| (value.type === AST_NODE_TYPES.Literal && (value.value == null || (typeof value.value === "number" && value.value <= 0))) || | ||
| (value.type === AST_NODE_TYPES.UnaryExpression && value.operator === "-" && value.argument.type === AST_NODE_TYPES.Literal && typeof value.argument.value === "number") || | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The 💡 Suggested fixEvaluate the unary expression numerically instead of only checking (value.type === AST_NODE_TYPES.UnaryExpression &&
(value.operator === "-" || value.operator === "+") &&
value.argument.type === AST_NODE_TYPES.Literal &&
typeof value.argument.value === "number" &&
(value.operator === "-" ? -value.argument.value : +value.argument.value) <= 0)And add a regression test case: { code: `const { execSync } = require("child_process"); execSync("git status", { timeout: +0 });`, errors: [{ messageId: "requireTimeout" }] },@copilot please address this. |
||
| (value.type === AST_NODE_TYPES.Identifier && value.name === "undefined"); | ||
| if (!isMissingTimeout) return true; | ||
|
Comment on lines
+130
to
+134
|
||
| } | ||
|
|
||
| return false; | ||
|
|
@@ -147,7 +150,8 @@ export const requireSyncExecTimeoutRule = createRule({ | |
| }, | ||
| schema: [], | ||
| messages: { | ||
| requireTimeout: "{{method}}({{arg}}) has no `timeout` option. Pass `{ timeout: <ms>, ...otherOptions }` so a hung or runaway child process cannot block the job indefinitely.", | ||
| requireTimeout: | ||
| "{{method}}({{arg}}) has no positive `timeout` option. `timeout: 0` disables the timeout; pass `{ timeout: <positive milliseconds>, ...otherOptions }` so a hung or runaway child process cannot block the job indefinitely.", | ||
| }, | ||
| }, | ||
| defaultOptions: [], | ||
|
|
||
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.
[/tdd] The new zero/negative timeout test cases only cover
execSync.execFileSyncandspawnSyncshare the samehasTimeoutOptionpath but have no parallel coverage fortimeout: 0ortimeout: -1.💡 Suggested addition
Add at least one case per method to the existing
"invalid: execFileSync and spawnSync without timeout option"describe block:@copilot please address this.