Skip to content

Fix intermittent exit code capture failure in SSH executeCommand#11

Merged
jamesmurdza merged 2 commits into
jamesmurdza:masterfrom
synacktraa:fix/ssh-command-exit-code
Sep 24, 2025
Merged

Fix intermittent exit code capture failure in SSH executeCommand#11
jamesmurdza merged 2 commits into
jamesmurdza:masterfrom
synacktraa:fix/ssh-command-exit-code

Conversation

@synacktraa

@synacktraa synacktraa commented Sep 24, 2025

Copy link
Copy Markdown
Collaborator

Problem

The executeCommand method was intermittently returning exit code 0 for commands that should fail (e.g., nonexistentcommand12345 should return 127). This happened because we were relying on the SSH2 exit event, which is optional according to the SSH2 specification.

Test Case That Was Failing

const failResult = await sandbox.runCommand('nonexistentcommand12345');
expect(failResult.exitCode).toBe(127); // Sometimes got 0 instead of 127

Root Cause

  • SSH2 exit events are optional per the specification
  • When the exit event doesn't fire, we were falling back to exit code 0
  • This caused failed commands to appear successful intermittently

Solution

Stop relying on optional SSH2 events and capture exit codes at the command level:

  1. Append exit code capture to every foreground command: ${command}; echo "EXIT_CODE:$?"
  2. Parse from output by searching for the last line starting with EXIT_CODE:
  3. Clean the output by removing the exit code line before returning
  4. Maintain backward compatibility - no API changes

Testing

  • Commands that fail now consistently return correct exit codes
  • Commands that succeed still return exit code 0
  • Output remains clean (exit code line removed)
  • Background commands unaffected
  • Ran the failing test 10 times - all passes

Impact

  • Risk: Low - only changes internal implementation
  • Benefit: High - eliminates flaky test failures and unreliable exit codes
  • Compatibility: Full backward compatibility maintained

Replace optional SSH2 'exit' event with command-level exit code capture
by appending '; echo "EXIT_CODE:$?"' to commands and parsing from output.
The SSH2 spec makes 'exit' events optional, causing intermittent fallback
to exit code 0 for failed commands.

- Append exit code capture to all foreground commands
- Parse exit code from last matching line in output
- Remove exit code line from final output to keep it clean
- Eliminates race condition with optional 'exit' event
@synacktraa

Copy link
Copy Markdown
Collaborator Author

Made a new release with version 1.0.1 @jamesmurdza

@jamesmurdza

Copy link
Copy Markdown
Owner

All tests pass for me!

@jamesmurdza jamesmurdza merged commit e15a9b0 into jamesmurdza:master Sep 24, 2025
0 of 2 checks passed
@synacktraa synacktraa deleted the fix/ssh-command-exit-code branch September 24, 2025 12:09
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