Skip to content

fix(commands): do not accept a removed override when the task fires - #377

Open
PerryLink wants to merge 1 commit into
koishijs:mainfrom
PerryLink:fix/commands-deferred-accept
Open

PerryLink wants to merge 1 commit into
koishijs:mainfrom
PerryLink:fix/commands-deferred-accept

Conversation

@PerryLink

Copy link
Copy Markdown

Problem

init() guards on this.config[command.name] when it schedules the task, but
the task re-reads the same mutable map when it fires, one tick later:

init(command: Command) {
  if (!this.config[command.name]) return
  this._tasks[command.name] ||= this.ctx.setTimeout(() => {
    delete this._tasks[command.name]
    this.accept(command, this.config[command.name], true)   // may be undefined
  }, 0)
}

write() deletes the entry and then emits command-updated, which calls
init(). When a previous write has a task pending and the next write removes the
override, the pending callback fires against an empty map and accept destructures
undefined:

Uncaught TypeError: Cannot destructure property 'create' of 'override' as it is undefined.
    at CommandManager.accept (plugins/commands/src/index.ts:266:13)
    at <anonymous> (plugins/commands/src/index.ts:150:12)

ctx.setTimeout calls the callback directly inside a raw setTimeout, so this
escapes as an unhandled exception rather than a logged one. accept's third
parameter and its own contract ("create snapshot for restoration; override config
and options") both assume an override exists.

Changes

       delete this._tasks[command.name]
-      this.accept(command, this.config[command.name], true)
+      // the override may be removed before the task is executed
+      const override = this.config[command.name]
+      if (override) this.accept(command, override, true)

The callback still observes the freshest override — it only declines to run when
there is none, which is what the guard three lines above already states.

Regression coverage

plugins/commands/tests/index.spec.ts adds one case: add an alias, remove it,
then let the pending task fire.

On main it fails with the TypeError above (the uncaught exception also
aborts the run — 8 later cases never execute); with the change the file is
14 passing. corepack yarn yakumo tsc commands exits 0.

Scope

plugins/commands/src/index.ts only. accept() and its other two callers are
untouched, and no other package is involved.

`init()` guards on `this.config[command.name]` when it schedules the deferred
task, but the task re-reads the same map when it fires one tick later. When a
later write removes the override, the pending callback calls `accept` with
`undefined` and destructuring throws:

    Uncaught TypeError: Cannot destructure property 'create' of 'override' as it is undefined.
        at CommandManager.accept (plugins/commands/src/index.ts:266:13)
        at <anonymous> (plugins/commands/src/index.ts:150:12)

`ctx.setTimeout` invokes the callback inside a raw `setTimeout`, so the throw
escapes as an unhandled exception. Read the override into a local and skip the
call when it is gone; the callback still observes the freshest value.

This branch has not been deployed

No deployments
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.

1 participant