Skip to content

interp: make the timeout recoverable, per package initializer - #5587

Open
0pcom wants to merge 1 commit into
tinygo-org:devfrom
0magnet:interp-recoverable-timeout
Open

interp: make the timeout recoverable, per package initializer#5587
0pcom wants to merge 1 commit into
tinygo-org:devfrom
0magnet:interp-recoverable-timeout

Conversation

@0pcom

@0pcom 0pcom commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

A package initializer that cannot be computed at compile time — crypto AES key expansion, secp256k1 table setup — runs the interpreter out of its budget and fails the build:

interp: running for more than 3m0s, timing out (executed calls: ...)

The only recourse today is raising -interp-timeout for everything, which moves the cliff rather than removing it, and slows every other build to accommodate one package.

This makes the timeout an ordinary recoverable error. The initializer that exceeded its budget reverts to a runtime call — exactly what would happen on native Go — and the build continues. errTimeout joins the existing set of recoverable errors (errLoopUnrolled, errLoopTooLong, errInvalidPtrToIntSize), so it takes the same path those already do.

Each initializer also gets its own budget rather than sharing one clock across the whole run. Previously a single expensive initializer could consume the budget and leave the packages after it with no time for precomputation they could have done.

Under -debug it reports which package timed out and after how long, since "this init now happens at startup instead of at compile time" is a performance fact worth being able to see.

Verified: go test ./interp/ passes, and a program with an initializer heavy enough to exhaust the budget now builds and runs (printing the value its init computed) where it previously failed the build.

@dgryski

dgryski commented Aug 14, 2026

Copy link
Copy Markdown
Member

Previously #2384

A package initializer that cannot be computed at compile time — crypto
AES key expansion, secp256k1 table setup — would run the interpreter out
of its budget and fail the BUILD. The only recourse was to raise the
timeout for everything, which just moves the cliff.

The timeout is now an ordinary recoverable error: the initializer that
exceeded it reverts to a runtime call, exactly as it would on native Go,
and the build continues. Each initializer also gets its own budget rather
than sharing one, so a single expensive one no longer starves the
remaining packages of precomputation they CAN do.

Under -debug it says which package timed out and after how long, because
"this init is now happening at startup instead of compile time" is a
performance fact worth being able to see.
@0pcom
0pcom force-pushed the interp-recoverable-timeout branch from 5b23570 to 9be25f9 Compare August 17, 2026 01:04
@0pcom

0pcom commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the pointer — I had not seen #2384, and reading it changes what I think this PR should be.

First, an apology: the branch was carrying things it should not. Rebased on dev, it was reverting real upstream work — the sync.OnceFunc(program.Build) SSA change, the normalizeIR test refactor, the LLVM 22 golden-file updates, and 59 lines of machine_esp32.go including the RTC pullup/pulldown fix. None of that was intended and none of it belongs in a PR about interp. The branch is now just the three interp files, rebased on dev; go test -tags=llvm22 ./interp/ passes.

On the substance. The objection that closed #2384 applies to this PR too, and I should say so plainly rather than let it be rediscovered. @aykevl's point was that timing out makes builds non-reproducible, and @niaow's was the same. This change arguably makes that worse, not better: today the wall clock decides whether the build fails, which is at least loud. With this, the wall clock decides what the binary contains — a fast machine precomputes an initializer, a loaded one defers it to runtime startup. Same source, two different outputs, silently. That is exactly what was rejected.

The fix you and @niaow converged on there was a counter, and r.callsExecuted already exists. A per-initializer call budget would make the decision fully deterministic — same source, same choice, every machine — with the wall clock left as the pathological-case guard it is now. I am happy to rewrite it that way. I have not, because @aykevl's reservation covered counters too, and because of the question he asked and never got a full answer to:

can you give an example of code that fails in such a way? There is probably an interp bug that can be fixed.

Here is that example, which is what prompted this. Two initializers reliably exhaust a 3-minute budget: AES key expansion in crypto/aes, and secp256k1 precomputed table setup (github.com/btcsuite/btcd/btcec, via github.com/skycoin/skycoin/src/cipher). Both are large table constructions in a loop with no I/O and no external calls. Neither is exotic, and both compute fine at runtime.

If that is an interp bug worth fixing, then fixing it is better than any budget and this PR should be closed. If it is simply more computation than interp should be asked to do at build time, then the counter version is the smallest honest change and I will send it. Your call — I would rather not re-litigate #2384 by accident.

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.

2 participants