Skip to content

fix: release foreground bash shells after completion#135

Merged
frostming merged 2 commits intobubbuild:mainfrom
Andy963:fix/foreground-bash-release
Mar 27, 2026
Merged

fix: release foreground bash shells after completion#135
frostming merged 2 commits intobubbuild:mainfrom
Andy963:fix/foreground-bash-release

Conversation

@Andy963
Copy link
Copy Markdown
Contributor

@Andy963 Andy963 commented Mar 25, 2026

Summary

Foreground bash commands are now released from ShellManager after completion, preventing finished shells (and their buffered stdout/stderr) from being retained in memory in long-running Bub processes (e.g. chat / gateway).

Changes

  • Add ShellManager.release(shell_id) to remove a shell from the internal registry.
  • Ensure foreground bash always calls release() in a finally block (success, non-zero exit, timeout).
  • Add regression tests to assert no foreground shells remain in _shells after execution (including failure).

Notes

  • Background shell lifecycle is unchanged (still accessible via bash.output / bash.kill).

Verification

  • uv run ruff check .
  • uv run mypy src
  • uv run pytest -q

raise KeyError(f"unknown shell id: {shell_id}") from exc

def release(self, shell_id: str) -> ManagedShell | None:
return self._shells.pop(shell_id, None)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it can be moved into wait_closed to make it cleaner, since wait_closed will be called on all paths

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly. The timeout path goes through terminate(), not wait_closed(), so wait_closed() is not called on all paths. This change is intentionally scoped to fixing foreground shell cleanup. Background shell cleanup is a separate concern, and I'm not sure yet where that cleanup should happen, since we haven't defined the lifecycle for completed background shells.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, yes, then move to _finalize_shell

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @Andy963 would you like to update the PR? Does my last comment make sense to you?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But moving release() into _finalize_shell() centralizes cleanup, but it still does not fully address background shells: a background shell that exits
naturally and is never touched again will still remain registered.

@frostming frostming merged commit 7ebc519 into bubbuild:main Mar 27, 2026
5 checks passed
@Andy963 Andy963 deleted the fix/foreground-bash-release branch March 29, 2026 12:36
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