Skip to content

Consistently close the terminal - #58

Merged
ernestrc merged 5 commits into
unstablebuild:mainfrom
HigherOrderLogic:main
Sep 22, 2026
Merged

ernestrc merged 5 commits into
unstablebuild:mainfrom
HigherOrderLogic:main

Conversation

@HigherOrderLogic

Copy link
Copy Markdown
Contributor

Previously, running exit in a terminal only closed it when it's the focused handler. This patches always close the terminal when it exits.

@ernestrc ernestrc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for taking the initiative to fix this.

In principle, I'm not opposed to broadcasting term.EventNone from the workspace manager to all workspaces, and from each browser to all of its windows and free tabs. That does seem appealing, but I'm not convinced by the exitReporter interface. I think it overcomplicates the logic and isn't future-proof: a single intermediate wrapper that doesn't implement Exited breaks the chain of interface type assertions. The lack of tests makes this even more concerning.

There are also cases this doesn't address, such as extension-provided tui.Handler implementations installed via RPC, which use handlerrpc.ClientStream[T] and have the same known limitation. Maybe the approach of sending term.EventNone to force a call to .Handle, thereby prompting collections to remove exiting children, has run its course.

The vte already knows when the shell exits (where we set e.exit in vte.Handler), and it already talks outward to the browser: browser.TabManager is passed into vte.NewHandler. We can declare a new interface in the vte package with just the method vte needs, and browser.Component satisfies it directly which it's already what gets passed as TabManager today.

// declared in package vte
type TabExiter interface {
	OnTabExit(uri workspaceapi.URI) bool
}

That gives us a compile-checked contract instead of an assertion chain, identity comes from the URI at call time, and it works the same for free tabs and for windows in non-focused workspaces; workspaceTabManager.SetTabName already hops scheduleNextTick and updates a workspace that isn't in focus.

Also, please always add tests that cover, at a minimum, the bug we're trying to fix. Otherwise, we risk reintroducing the issue in the future.

Comment thread internal/browser/component.go Outdated
}
exit, handled = c.union.Handle(ev)
if exit {
c.RemoveWindowContent(c.focus())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

c.focus() is read after c.union.Handle(ev), so it is not necessarily the window that exited.

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.

exit == true from union.Handle means that the window has exited successfully, and there are no other windows, meaning we're at the homepage of the editor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think making these kind of assumptions here is what could get us into subtle bugs territory. The semantics of exit are somewhat loose, specially for collections like union and wm.

In any case, OnTabExit already gets called unconditionally, so I think this branch is redundant. If it isn't add the test that proves that this branch is necessary, so anyone mistakenly deeming it as unnecessary in the future doesn't delete it by mistake and re-introduces the bug.

Comment thread internal/ide/async_vte.go Outdated
}

func (av *asyncVTE) Exited() bool {
if e, ok := av.real.(interface{ Exited() bool }); ok {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

clever, but not necessary. An anonymous interface type assertion should be a last resort.

Comment thread internal/browser/component.go Outdated
return c.union.Handle(ev)
if ev.Type == term.EventNone {
focusID := c.wm.Focus().ID()
for _, t := range slices.Clone(c.buffers) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need to clone the slice here. It's causing allocations for no real benefit.

Comment thread internal/browser/component.go Outdated
}
})
for _, id := range closing {
if bw, ok := c.findWindow(id); ok {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We already had the window, why not store a slice of *browserWindow, so we don't need to do a map lookup here?

Comment thread internal/browser/component.go Outdated
focusID := c.wm.Focus().ID()
for _, t := range slices.Clone(c.buffers) {
if t.free && t.Exited() {
t.Handle(ev)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

browser.Tab.Handle should be called when an event is dispatched to a particular tui.Handler.

This does what you intend it to do (*browser.Tab calling its parent browser and removing itself when it exits) but for a future reader of this code it might be quite hard to understand at first. I get that it's correct today, but we should strive to write code that it's easy to read for someone else in the future, no implicit contracts. In this same function, we're removing windows by calling *browserWindow.Close, removing tabs by calling *browser.Tab.Handle and removing tabs/content from a window again via *browser.Component.RemoveWindowContent.

@HigherOrderLogic
HigherOrderLogic force-pushed the main branch 2 times, most recently from 4f3615e to 77ea0c2 Compare September 16, 2026 16:30
Comment thread internal/term/vte/component.go Outdated
func (t *Component) Run(publisher browser.EventPublisher) error {
return t.run(publisher)
err := t.run(publisher)
if ex, ok := t.tm.(TabExiter); ok {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've verified it manually and now sleep 2 && exit on a vte and switching to another tile doesn't close the window anymore. I suspect there's an intermediate interface here that you forgot to update it to satisfy TabExiter; my biggest concern in your prior design/approach.

Add OnTabExit to browser.TabManager. This way the compiler will tell us if one of our TabManager implementations is missing OnTabExit, now and in the future.

Comment thread internal/term/vte/component.go Outdated
if ex, ok := t.tm.(TabExiter); ok {
uri := t.uri
if !t.cfg.ScheduleNextTick(func() { ex.OnTabExit(uri) }) {
ex.OnTabExit(uri)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if ScheduleNextTick ever returns false, this would cause a race. If we can't schedule events, we have bigger fish to fry than exiting a vte. Dropping the call is acceptable here.

return
}

func (c *Component) OnTabExit(uri workspaceapi.URI) bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The linter is failing here. OnTabExit needs a comment.

}

// Run must be called in a separate goroutine to start processing incoming
// data from the pty master.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We shouldn't remove this comment: it's causing the linter to fail

return append([]workspaceapi.URI(nil), r.exited...)
}

func TestHandlerCallsTabExiterOnPtyExit(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TestHandlerCallsTabExiterOnPtyExit only runs with make test-e2e because it's not a hermetic test (//go:build e2e). This might not be intuitive for you, so that's my bad. Normally when we add a test for such a small change, we should have it fail first, then update/fix, then see that i passes (TDD).

This means that make test covers none of your changes. There's no hermetic test of browser.Component.OnTabExit, and nothing tests the wiring, which is the part that's broken. The more loosely coupled the logic in browser.Component is (i.e. anonymous interface assertions), the more and better test coverage we must have.

Comment thread internal/browser/component.go Outdated
}
exit, handled = c.union.Handle(ev)
if exit {
c.RemoveWindowContent(c.focus())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think making these kind of assumptions here is what could get us into subtle bugs territory. The semantics of exit are somewhat loose, specially for collections like union and wm.

In any case, OnTabExit already gets called unconditionally, so I think this branch is redundant. If it isn't add the test that proves that this branch is necessary, so anyone mistakenly deeming it as unnecessary in the future doesn't delete it by mistake and re-introduces the bug.

Comment thread internal/browser/component.go Outdated
}
}
var exited *browserWindow
c.wm.Iterate(func(w thandler.Window) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

no need to iterate on c.wm windows, which gets you only tui.Handler. You can iterate directly on the browser windows, bypassing the need for the first interface assertion:

for _, bw := range c.windows {
		h, _ := bw.Content()
		u, ok := h.(uriHandler)
		if !ok || u.URI().String() != uri.String() {
			continue
		}
        if u.URI().String() == uri.String() {
           	exited, _ = c.findWindow(w.ID())
        }
		break
	}

The only interface assertion needed now is URIHandler, which I'd prefer it if it's defined as a private ifc elsewhere in this file, rather than type asserting an anonymous ifc.

Signed-off-by: HigherOrderLogic <[email protected]>
vte.Component.Run notifies browser.TabManager.OnTabExit whenever a pty child
exits, and vte.DefaultConfig().ScheduleNextTick runs that callback inline on the
vte run goroutine. Two doubles that were never reached before now crash the
hermetic suite: idetask.fakeBrowser embeds a nil browser.Browser, so the promoted
method nil-dereferences, and plugin.nopBrowser.OnTabExit panics "should not be
called" even though it is now reached by design.

Signed-off-by: ernestrc <[email protected]>
Auto-close relied on the focused handler observing exit=true from Handle, so a
terminal whose child exited while its window was not in focus stayed on screen
until the user happened to focus it. The test opens a terminal, moves focus to a
second window, and releases the child from Go so no key is ever typed, leaving
the push through browser.TabManager.OnTabExit as the only way the browser can
learn the terminal is gone.

It spans vte.Component.Run, tabNameAliaser resolving the pty URI to the tab key,
workspaceTabManager and browser.Component; each hop was otherwise only covered
against a stub. It lives beside TestE2EExoUserQuitAutoClosesTab, which covers the
focused case through the event-routing path.

Signed-off-by: ernestrc <[email protected]>
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 12.72727% with 48 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/browser/browsertest/browser_gomock.go 0.00% 24 Missing ⚠️
internal/browser/component.go 0.00% 12 Missing ⚠️
internal/term/vte/component.go 20.00% 4 Missing ⚠️
cmd/xsandbox/internal/sandbox/browserhost.go 0.00% 2 Missing ⚠️
internal/browser/browsertest/browser.go 0.00% 2 Missing ⚠️
internal/term/vte/replay.go 0.00% 2 Missing ⚠️
internal/text/component.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@ernestrc
ernestrc merged commit 216b553 into unstablebuild:main Sep 22, 2026
7 of 8 checks passed
ernestrc added a commit that referenced this pull request Sep 25, 2026
Since #58 terminals close when their process exits, so the `echo abc` terminal
was gone before the test could snapshot it.

Signed-off-by: ernestrc <[email protected]>
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.

3 participants