Commit 698475e
authored
wazero: fix guard shutdown leak, logging namespace, and typed trap detection (#3790)
Three actionable improvements to the wazero WASM guard integration
identified in a Go module review: WASM runtimes were never closed on
shutdown, `registry.go` was emitting logs under the wrong namespace
(`guard:context` instead of `guard:registry`), and trap detection relied
on fragile string matching.
## Changes
### `Registry.Close()` + shutdown wiring
- Added `Close(ctx context.Context)` to `Registry` — iterates guards and
calls `Close(ctx)` on those that implement it
- Called from both `UnifiedServer.Close()` and `InitiateShutdown()`
(nil-safe) to release WASM JIT runtime resources on shutdown
- Previously `WithCloseOnContextDone(true)` was effectively inert since
guards were created with `context.Background()`
### Fix logging namespace in `registry.go`
- `registry.go` was calling `log.Printf(...)` which resolved to the
package-level `log` var declared in `context.go` with namespace
`"guard:context"` — meaning `DEBUG=guard:registry` silently dropped
those messages
- Replaced with `logger.LogInfo("guard", ...)` for operational events;
retained `debugLog.Printf` for debug-only paths
- Removed a duplicate debug log line introduced in the fix
### Typed `sys.ExitError` check in `isWasmTrap`
- Old implementation: `strings.Contains(err.Error(), "wasm error:")` —
fragile against wazero message format changes, and would incorrectly
poison a guard on a clean `exit(0)` (e.g. TinyGo init)
- New implementation checks `*sys.ExitError` via `errors.As` first; only
non-zero exit codes are treated as traps:
```go
func isWasmTrap(err error) bool {
if err == nil {
return false
}
var exitErr *sys.ExitError
if errors.As(err, &exitErr) {
return exitErr.ExitCode() != 0
}
return strings.Contains(err.Error(), "wasm error:")
}
```
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `example.com`
> - Triggering command: `/tmp/go-build900194802/b514/launcher.test
/tmp/go-build900194802/b514/launcher.test
-test.testlogfile=/tmp/go-build900194802/b514/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -w o4Cr-wAQW cfg x_amd64/vet -c
-I /tmp/go-build185-bool x_amd64/vet 1085�� elemetry.io/otel-errorsas
cfg x_amd64/vet --gdwarf-5 ions =0 x_amd64/vet` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build900194802/b496/config.test
/tmp/go-build900194802/b496/config.test
-test.testlogfile=/tmp/go-build900194802/b496/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build900194802/b389/vet.cfg g_.a
ache/go/1.25.8/x64/src/bufio/bufio.go x_amd64/vet --gdwarf-5
1085977/b140/_cg-atomic -o x_amd64/vet -W _.a
/tmp/go-build185-ifaceassert x_amd64/vet . .io/otel/attribu-atomic --64
x_amd64/vet` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build900194802/b514/launcher.test
/tmp/go-build900194802/b514/launcher.test
-test.testlogfile=/tmp/go-build900194802/b514/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -w o4Cr-wAQW cfg x_amd64/vet -c
-I /tmp/go-build185-bool x_amd64/vet 1085�� elemetry.io/otel-errorsas
cfg x_amd64/vet --gdwarf-5 ions =0 x_amd64/vet` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build900194802/b514/launcher.test
/tmp/go-build900194802/b514/launcher.test
-test.testlogfile=/tmp/go-build900194802/b514/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -w o4Cr-wAQW cfg x_amd64/vet -c
-I /tmp/go-build185-bool x_amd64/vet 1085�� elemetry.io/otel-errorsas
cfg x_amd64/vet --gdwarf-5 ions =0 x_amd64/vet` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build900194802/b523/mcp.test
/tmp/go-build900194802/b523/mcp.test
-test.testlogfile=/tmp/go-build900194802/b523/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -o 1085977/b432/_pkg_.a -trimpath
x_amd64/vet us.pb.go t/unicode/bidi -lang=go1.25 x_amd64/vet` (dns
block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/github/gh-aw-mcpg/settings/copilot/coding_agent)
(admins only)
>
> </details>5 files changed
Lines changed: 153 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
20 | 35 | | |
21 | 36 | | |
22 | 37 | | |
| |||
450 | 465 | | |
451 | 466 | | |
452 | 467 | | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
453 | 532 | | |
454 | 533 | | |
455 | 534 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | | - | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | | - | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
| 79 | + | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
106 | 133 | | |
107 | 134 | | |
108 | 135 | | |
| |||
116 | 143 | | |
117 | 144 | | |
118 | 145 | | |
119 | | - | |
| 146 | + | |
120 | 147 | | |
121 | 148 | | |
122 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
830 | 831 | | |
831 | 832 | | |
832 | 833 | | |
833 | | - | |
834 | | - | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
835 | 839 | | |
836 | | - | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
837 | 850 | | |
838 | 851 | | |
839 | 852 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
1152 | 1153 | | |
1153 | 1154 | | |
1154 | 1155 | | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
1155 | 1176 | | |
1156 | 1177 | | |
1157 | 1178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
718 | 718 | | |
719 | 719 | | |
720 | 720 | | |
721 | | - | |
| 721 | + | |
722 | 722 | | |
723 | 723 | | |
724 | 724 | | |
| |||
753 | 753 | | |
754 | 754 | | |
755 | 755 | | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
756 | 761 | | |
757 | 762 | | |
758 | 763 | | |
| |||
0 commit comments