Commit Graph
1 Commits
Author SHA1 Message Date
JoeyWu-tech d10cdf7969 agentplug: a late timeout no longer fails every connect after it (#3727)
An agent module loads and connects under limit, whose timer interrupts
the module once its second has passed. When the timer fired just as the
call returned, Stop didn't wait for its func, which could still be on
its way to Interrupt when limit read fired. The interrupt then landed on
an idle runtime, and moejs keeps one that lands while nothing runs for
the next call, whatever it is (its Interrupt doc). That call's own limit
never fired, so nothing cleared it: the agent's every connect after
failed with "connect: interrupted: took too long" until magpie loaded
the module again. A module that takes about its second, on a busy
machine, is the case.

limit now waits for the timer's func when Stop says it has begun, then
clears the interrupt it gave. A call the timer ran out on reads took too
long when it failed, as before, and keeps what it returned when it
finished; before, which of the two it got depended on the same race.
afterFunc stands in for time.AfterFunc, so a test can hold the timer's
func.

internal/middleware's timer func can run late the same way; there it
stops only the next call, not every one after: #3742.

What was run (tree 0199ae4d, on origin/main 68ee4117, go1.26.8,
linux/amd64):
- TestLimitRunsOutAsTheCallEnds, new: in a synctest bubble the timer's
  func is held as the call returns. limit hasn't returned while it is
  held; once it is let go, a call that finished keeps its nil, one that
  failed reads took too long, and the connect after answers at once.
- TestLimitStopsAModuleStillRunning, new, green with main's limit too:
  limit's everyday case. A connect that never ends, at once or after an
  await, reads "connect: took too long" and the three connects after it
  answer; a load that never ends reads took too long.
- Each 50 of 50 ok, and -race -count=40 -cpu 1,2 ok with no race.
- Broken by hand, each red with its own message:
  - main's limit, through afterFunc: both cases, limit returning while
    the func was held and the connect after failing "connect:
    interrupted: took too long", and the failed call returning the
    module's own error;
  - no wait for the timer's func: both, returning while it was held,
    and the connect after;
  - the interrupt cleared before the wait: both, the connect after;
  - the interrupt also cleared as each call starts, with no wait:
    both, returning while the func was held;
  - no clear: both tests, every connect after;
  - the timer never stopped, so every call waits for its func: both
    cases, the connect after taking its whole second;
  - a call the timer ran out on always reading took too long: the one
    that finished;
  - one that failed keeping its own error: the one that failed, and
    TestLimitStopsAModuleStillRunning's three cases, with the runtime's
    "interrupted: took too long".
- The issue's case, with a pause put into the timer's func before it
  interrupts, a connect that busy-waits 100ms and connectLimit at 50ms
  for that one call: on 68ee4117 the three connects after all failed
  "connect: interrupted: took too long"; here that call returned once
  the pause ended, and all three answered.
- gofmt -l lists nothing; GOOS=linux, windows and darwin go vet -tags
  nogui ./..., GOOS=windows go vet ./..., GOOS=linux and darwin go build
  -tags nogui ./... and GOOS=windows go build ./...: exit 0 each. go vet
  and go build without the tag on linux: exit 1, pkg-config finding no
  fontconfig, libsoup-3.0 or gio-unix-2.0 (no GTK or WebKitGTK headers
  on this box).
- Full suite, -tags nogui (CI's Linux job tags gtk3), outside graftcp
  (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout
  45m, log kept): 66 ok, internal/agentplug among them, and 7 red, none
  in what this changes:
  - agent TestCodexDriftReached and TestCodexBehindReverseProxy: #3398;
    TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI,
    TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
    TestQoderSettingsFromAnIDEPluginIsNotTheCLI,
    TestOmpDetectedWithPiDir and TestZedAppPath: #3141;
    TestReasonixCLIRequiresSupportedNativeVersion and
    TestReasonixCLIRecoversAfterProbeTimeout: this box's /tmp/.git,
    #3641.
  - davsync TestSyncTeraCloudStillSeesOtherWrites: #3420.
  - edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram: this
    box's umask 077, #3640.
  - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git,
    #3675.
  - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie:
    this box's millions of open fds, #3649.
  - provider: "fatal error: close of synctest channel from outside
    bubble", #3632; the package's tests after it didn't run.

Not tried: Windows or macOS, and a real plugin's module that takes
about its second; the timing is injected.

Reviewed on macOS arm64, go1.26.8, rebased on 430e3050d: -race
-count=40 -cpu 1,2 on internal/agentplug ok (and -count=20 again after
the rebase). Broken by hand: main's limit through afterFunc turned both
new cases red with the messages above; dropping ClearInterrupt turned
all four red. internal/plugin, internal/library and internal/cli ok;
gofmt clean; vet ok for linux nogui, windows, darwin. Not run:
internal/agent (slow, no change there beyond the import).
2026-10-27 15:52:01 +08:00