Repository navigation
Consistently close the terminal - #58
Conversation
cb778c8 to
4f3615e
Compare
ernestrc
left a comment
There was a problem hiding this comment.
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.
| } | ||
| exit, handled = c.union.Handle(ev) | ||
| if exit { | ||
| c.RemoveWindowContent(c.focus()) |
There was a problem hiding this comment.
c.focus() is read after c.union.Handle(ev), so it is not necessarily the window that exited.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| func (av *asyncVTE) Exited() bool { | ||
| if e, ok := av.real.(interface{ Exited() bool }); ok { |
There was a problem hiding this comment.
clever, but not necessary. An anonymous interface type assertion should be a last resort.
| return c.union.Handle(ev) | ||
| if ev.Type == term.EventNone { | ||
| focusID := c.wm.Focus().ID() | ||
| for _, t := range slices.Clone(c.buffers) { |
There was a problem hiding this comment.
No need to clone the slice here. It's causing allocations for no real benefit.
| } | ||
| }) | ||
| for _, id := range closing { | ||
| if bw, ok := c.findWindow(id); ok { |
There was a problem hiding this comment.
We already had the window, why not store a slice of *browserWindow, so we don't need to do a map lookup here?
| focusID := c.wm.Focus().ID() | ||
| for _, t := range slices.Clone(c.buffers) { | ||
| if t.free && t.Exited() { | ||
| t.Handle(ev) |
There was a problem hiding this comment.
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.
4f3615e to
77ea0c2
Compare
| func (t *Component) Run(publisher browser.EventPublisher) error { | ||
| return t.run(publisher) | ||
| err := t.run(publisher) | ||
| if ex, ok := t.tm.(TabExiter); ok { |
There was a problem hiding this comment.
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.
| if ex, ok := t.tm.(TabExiter); ok { | ||
| uri := t.uri | ||
| if !t.cfg.ScheduleNextTick(func() { ex.OnTabExit(uri) }) { | ||
| ex.OnTabExit(uri) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
| } | ||
| exit, handled = c.union.Handle(ev) | ||
| if exit { | ||
| c.RemoveWindowContent(c.focus()) |
There was a problem hiding this comment.
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.
| } | ||
| } | ||
| var exited *browserWindow | ||
| c.wm.Iterate(func(w thandler.Window) { |
There was a problem hiding this comment.
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]>
77ea0c2 to
2a061d4
Compare
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 Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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]>
Previously, running
exitin a terminal only closed it when it's the focused handler. This patches always close the terminal when it exits.