Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·6 min read

TaskPad's session restore could register a window it never showed

The restore code in TaskPad created a window first and decided afterwards whether it had anything to show. That order is fine while every saved file still exists. It stops being fine the moment one does not, and the failure mode is the worst kind for an editor that promises never to lose your work: a window that exists, is tracked, and is not on screen.

Project
TaskPad
Date
Oct 2026
Stack
C#, WPF
Commit
48b16bf

What the session file promises

TaskPad has what its source calls hot exit. It remembers every window, split layout, tab and cursor position, and keeps unsaved or untitled text in session\backup-*.txt files, so closing the app never loses work and reopening brings it all back. The session file is plain text: tab separated rows of three kinds, doc, win and grp. A win row carries bounds, maximized flag and layout, and is followed by one grp row per editor group listing its tabs.

The feature arrived in 15a5c06, "restore windows, tabs and unsaved text on start". The fix I am writing about is 48b16bf, made the same day.

The shape of the old code

The original Restore() was a single loop over the rows with a switch. A doc row loaded a document. A win row finished the previous window and started a new one:

csharp
case "win":
{
    FinishWindow(win, groups, activeGroup);
    win = new TaskWindow(false)
    {
        Left = D(p[1], 100), Top = D(p[2], 100),
        Width = D(p[3], Workspace.Settings.Width), Height = D(p[4], Workspace.Settings.Height),
    };
    if (p[5] == "1") win.WindowState = WindowState.Maximized;
    groups = win.ApplyLayout(p[6]);
    ...
}

The window object exists from this line on. TaskWindow registers itself in Workspace.Windows when it is created, and only FinishWindow called Show(). In between, every grp row tried to open its tabs by document id, and a tab whose document had not loaded was skipped with continue. Documents fail to load for ordinary reasons: the file was deleted or moved since the last run.

FinishWindow did try to clean up. It removed groups left empty, closed the window if TabCount == 0, and otherwise called Show(). The weak point is the outer structure:

csharp
catch
{
    return Workspace.Windows.Count > 0;
}

If anything threw anywhere in the loop, from a bad number in a row to a layout string that did not parse, the catch returned whether any window was registered. A window created by a win row and not yet finished was registered and never shown. Restore reported success, and the workspace now held a window nobody could see.

Why an invisible window is worse than a crash

Two other places in TaskPad trusted Workspace.Windows.

The first is saving. The session is rewritten a debounced two seconds after changes, and the writer iterated every window in the workspace. An invisible window with tabs would be written back into the session file, so the problem would survive restarts.

The second is the single-instance handler in Program.cs. When you double-click a file while TaskPad is running, the second process passes the paths over to the first, which picked its target like this:

csharp
var w = Workspace.LastActive ?? (Workspace.Windows.Count > 0 ? Workspace.Windows[0] : null);
if (w == null) { w = new TaskWindow(); w.Show(); }
w.ReceiveFromOtherInstance(paths);

If LastActive or the first window was one of the hidden ones, the file would be delivered to a window that is not on screen, and the double-click would seem to do nothing. I did not capture that happening as a user report. I found it by reading what the restore path could leave behind and following who consumed Workspace.Windows.

The fix: parse, validate, create

48b16bf rewrote Restore() in three stages.

First, parse every row and load every document up front into a dictionary. Nothing is displayed yet.

Second, for each win block, gather its grp rows and work out the tabs that can actually be restored:

csharp
var groupTabs = grps.Select(g => TabsOf(g, docs)).ToList();
if (groupTabs.All(t => t.Count == 0)) continue;   // nothing to show: don't create a window at all

A window with no restorable tabs is never constructed. TabsOf also tolerates a missing document by skipping that entry, using TryParse and TryGetValue where the old code used int.Parse and an unchecked lookup.

Third, RestoreWindow builds the window and keeps the layout only if every group has tabs. Otherwise it falls back to one group holding all tabs. Bounds are checked as well. A width under 360 or a height under 240 falls back to the saved settings, and a position is used only if OnScreen says it lands within the virtual screen, otherwise the window is centered. The old code defaulted a missing position to 100, 100 and trusted whatever else it was given.

Its own try wraps the work, and failure is rolled back:

csharp
catch (Exception ex)
{
    Log(ex);
    foreach (var t in win.Groups.SelectMany(g => g.Tabs).ToList()) t.Detach();
    Workspace.Windows.Remove(win);
    return null;
}

The error goes to TaskPad-error.log in the temp folder instead of disappearing, the tabs are detached from their documents, and the window is removed from the workspace.

As a last guard, after the loop Restore() sweeps the workspace once more:

csharp
foreach (var w in Workspace.Windows.Where(w => !w.IsVisible).ToList()) Workspace.Windows.Remove(w);
foreach (var d in docs.Values.Where(d => d.Views.Count == 0).ToList()) d.Dispose();
if (Workspace.Windows.Count == 0) return false;

Returning false when nothing survived lets the caller in Program.cs take its normal no-session path instead of trusting a count.

The two consumers

The same commit makes the readers refuse hidden windows. The session writer now skips them: if (!w.IsVisible || groups.All(g => g.Tabs.Count == 0)) continue;. The single-instance handler prefers LastActive only when it is visible, falls back to the last visible window, and creates a new one if there is none.

What I took from it

Object construction that registers itself globally is a commitment. If creation and presentation are separate steps, every failure between them has to undo the registration, and the safest way to guarantee that is to not create the object until you know it can be shown. The rest of the fix was making everyone downstream agree on what exists: if a window is not visible, it is not part of the workspace.

#wpf#csharp#session-restore#debugging

Keep reading

Have a project in mind?