diff --git a/docs/changes/README.md b/docs/changes/README.md index ae26726..cab8465 100644 --- a/docs/changes/README.md +++ b/docs/changes/README.md @@ -28,6 +28,7 @@ One file per piece of work, named for it: | `remote-ops.md` | Commands on many servers, archive and search actions | | `mosh.md` | Mosh | | `localization.md` | Translations | +| `bridge-stop-status.md` | AI Bridge: a stopped command's exit status | Write for the people using the app, in the sections `CHANGELOG.md` uses: `### Added`, `### Changed`, `### Fixed`, `### Security`. One line per change, diff --git a/docs/changes/bridge-stop-status.md b/docs/changes/bridge-stop-status.md new file mode 100644 index 0000000..de7f086 --- /dev/null +++ b/docs/changes/bridge-stop-status.md @@ -0,0 +1,2 @@ +### Fixed +- A command the AI Bridge stopped is now always reported with the stop's own exit status. Before, it was sometimes reported with a different number. diff --git a/lib/services/bridge/bridge_tools.dart b/lib/services/bridge/bridge_tools.dart index c2b77fb..5dc61fb 100644 --- a/lib/services/bridge/bridge_tools.dart +++ b/lib/services/bridge/bridge_tools.dart @@ -82,7 +82,12 @@ class BridgeCommands { /// when a trapped signal arrives) is repeated while the command's process /// is still there. A command that died of a signal has been reaped by /// then, so its status is kept; a zombie `kill -0` still sees is reaped - /// by the next `wait`, so the loop cannot spin. A stopped command's + /// by the next `wait`, so the loop cannot spin. Some shells have already + /// collected the command by the time the cut-short `wait` returns, so + /// `kill -0` finds nothing; one more `wait` then gives the status the + /// shell kept. When the shell kept none (127), the first status stands + /// for a command that ended by itself, and a stop that had begun counts + /// as the stop's TERM. A stopped command's /// status is the stop's (143 for the TERM, 137 for the KILL). /// /// The app holds the channel's stdin open for the whole run and closes it @@ -117,6 +122,7 @@ exec 3<&- wait $p r=$? while [ "$r" -gt 128 ] && kill -0 $p 2>/dev/null; do wait $p; r=$?; done +if [ "$r" -gt 128 ]; then wait $p 2>/dev/null; q=$?; if [ "$q" -ne 127 ]; then r=$q; elif [ -n "$s" ]; then r=143; fi; fi if [ -z "$s" ]; then kill -s KILL $w 2>/dev/null; fi exit $r'''; return 'sh -c ${posixSingleQuote(wrapper)} ssg-bridge ' diff --git a/test/bridge_stop_test.dart b/test/bridge_stop_test.dart index 97a78e7..9c26161 100644 --- a/test/bridge_stop_test.dart +++ b/test/bridge_stop_test.dart @@ -156,13 +156,18 @@ void main() { final web = rig.serve( 'web1', (_) => FakeExecSession(stdout: _probeOutput), - delay: const Duration(milliseconds: 300), + delay: const Duration(seconds: 3), ); + // The channel takes 3 s to open and the call has 100 ms. The bound is + // well clear of both, so a slow runner cannot fail it, while a call + // that waited for the channel still would. final watch = Stopwatch()..start(); final r = await rig.call('server_stats', {'host': 'web'}); - expect(watch.elapsedMilliseconds, lessThan(280)); + expect(watch.elapsedMilliseconds, lessThan(2000)); expect(r.isError, true); - await Future.delayed(const Duration(milliseconds: 400)); + await Future.delayed( + Duration(milliseconds: 3400 - watch.elapsedMilliseconds), + ); expect(web.sessions.single.stdinClosed, true); expect(web.sessions.single.wasClosed, true); });