microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
94.31k stars 8.17k forks source link

Problems with session startup and resumption #16487

Open kamrann opened 6 months ago

kamrann commented 6 months ago

Windows Terminal version

1.18.3181.0

Windows build number

10.0.19045.0

Other Software

No response

Steps to reproduce

Expected Behavior

My expectation would be that restoration of the previous session should apply only to the first Terminal instance (be it at machine startup or subsequently). Any further explicitly opened instances should open a tab with the default profile, whereas any instances opened to house an application that was launched explicitly should simply have a single tab for that application.

Actual Behavior

Initial startup is as expected (for the most part, see caveat below), however I'm seeing strange behaviour following that. With a single instance of Terminal open, if I attempt to launch any console application (cmd.exe, PS, or a console program launched via Visual Studio debugger), that application will open in a new Terminal window as expected, but alongside it a further Terminal window will be opened populated with tabs based on the prior session (so to clarify, I end up with 3 windows: the original session, a duplication of it, and the opened application).

Regarding above noted caveat. It's not clear exactly how Terminal decides/updates what constitutes the previous session, but it seems far too easy for what I want to be considered my previous session to get replaced. Perhaps it's just based on the last window to be closed down? I really need some way to mark my 'main' session so that I don't end up losing the layout as a result of having at some point opened a secondary session. In particular, the fact that a session resulting from a request to open cmd.exe or similar directly can end up overwriting my Terminal layout restoration seems wrong.

Mubashir2611 commented 6 months ago

Can I contribute?

zadjii-msft commented 6 months ago

Hmm. You wouldn't happen to be launching the Terminal as admin at any point in all this, would you? There's separate persisted state for each of those two instances, so that might be at play here/?

Sounds like there's something weird going on with defterm here, since it's like the defterm launch is then also triggering another state restore?

I'd reckon this may almost be related to #15509? Or maybe, more horrifyingly, there's an interplay here between defterm, session restore, and refrigeration. I coulda swore we had a better dupe somewhere for "a defterm session seemingly can't communicate with the existing WT process", but I can't find that.

Just to confirm - do you only have one version of the Terminal installed? (e.g. only Stable, not also Preview)

kamrann commented 5 months ago

Not opening as admin, no. I've tested a bit and found that I don't see this behaviour on my laptop running Windows 11, but only on my other machine running Windows 10, on which I believe I updated the version of Windows Terminal at some point.

As far as I'm aware I only have a single version installed. I'm not too familiar with update process for Windows apps/Powershell though. Are there any commands you suggest I run to verify that the installations are as they should be?

zadjii-msft commented 5 months ago

I need to go dedupe the set of

issue Windows? DefTerm? Restore prev session Launch on startup? issues?
#16487 10 true true true ❌ launch on boot restores normally, then defterm also restores?
#16487 10 true true true ✅ seemingly works fine?
#16557 10 ? true true ❌ launch on boot restores normally, then {{a second launch, unsure if defterm}} also restores?
#16561 10 ? ? (probably) true ❌ launch on boot restores double of the session
#16732 10 true true ? ❌ When opening a defterm even with window open, creates a window with a set of "restored" tabs
zadjii-msft commented 5 months ago

Seems like there's something up with the way Windows 10 restores the Terminal... we restore the session, but then a subsequent launch of the Terminal also tries to restore. Weird.

Oh no, I can't repro this in a VM, least, not on 10.0.19041 & 1.19.2682.0

zadjii-msft commented 5 months ago

oh man I wonder if there's some other console app that's starting at boot, triggering defterm, which triggers a session restore. But then also the Terminal gets launched, and it simultaneously finds no one else booted, so it also session restores. Is there anything else suspicious in "Startup" in Task Manager?

zadjii-msft commented 4 months ago

Details in #16732 make me think that this is less related to Start on Login and more related to defterm accidentally triggering a session restore.

zadjii-msft commented 3 months ago

Note to self: Go look at https://github.com/microsoft/terminal/pull/16598#discussion_r1543634396

holy sh--, multi-window restore starts WindowsTerminal.exe which then gets a handle to our Monarch and sends it a commandline.

What the heck.

We could just... use Monarch directly here. I know you didn't write this code but I think we missed it when we migrated to single-proc.