Skip to content

Shell Start and Stop

Note

The order in which the shell starts, what stops it from starting, how it runs with no windows, and how it stops. It is the runtime view of what Process Model and Threading and Data Flow sketch. Decided: one process and one tre loop (ADR-0003 Process Model), settings.tdl read first and a bad one stops the shell (Settings File; settings.tdl is the confirmed name, with the root block Settings { }, D20, ADR-0014 Shell File Language > Decisions on the Open Items (2026-10-30)), run(keep_alive=True) and LoopHandle.stop() (TRE-REQ-005 Keep-Alive and Loop Stop), stay idle with no windows. Everything else here is proposed, with a recommendation.

Purpose and Scope

  • Define the start sequence, the failure rule at each step, the running state, the stop sequence, and the process rules that surround them: one instance, signals, exit codes.
  • Not in this note: how files are found and the registry works (Config Discovery and Registry), how errors are worded (Validation and Errors), reloading a running shell (Hot Reload), the command line and the control socket (IPC and CLI, to write), and how the shell is launched at login (70 Delivery, to write).

Behaviour

Process Rules

  • One process, one loop. The shell is one Python process; tre's App.run() owns the main thread; everything that touches a window or node runs there (Process Model and Threading > The rules).
  • One instance per session (decided 2026-10-28). Two shells would fight over the registry, the D-Bus names the shell owns and the same surfaces. The shell takes a lock in $XDG_RUNTIME_DIR/tesserae-shell/ on start. A second start finds the lock, prints that a shell is already running with its process id, and exits with code 4. The control commands go over the shell's D-Bus interface, not a socket (ADR-0013 Control Interface, accepted 2026-10-28; IPC and CLI). Letting a second start ask the first to reload was rejected. The runtime directory rules are in the next item.
  • The runtime directory is required (decided 2026-10-28). The shell needs XDG_RUNTIME_DIR, for its lock file and because a Wayland client finds the compositor socket there when WAYLAND_DISPLAY is a relative name (from libwayland, not read for this note). It does not fall back to another directory, as the XDG base directory spec allows: a fallback such as /tmp would make the lock and the socket reachable by other users, and without the directory the shell has no Wayland connection anyway. In pre-flight the shell checks:
  • the variable is set, non-empty and an absolute path, and the directory exists: otherwise exit code 2 naming the variable and the problem;
  • the directory is owned by the current user: otherwise exit code 2;
  • the mode is 0700 (the spec requires it): otherwise a warning, and the shell continues. The shell creates $XDG_RUNTIME_DIR/tesserae-shell/ with mode 0700 for the lock file. It disappears at logout, which suits a lock that must not outlive the session.
  • Cleanup. The spec says files not accessed for 6 hours may be cleaned unless their sticky bit is set, so the shell sets the sticky bit on the lock file instead of running a timer that would wake the idle loop. #needs-verification whether any cleaner runs on the target session, and that the bit is honoured on these files.
  • Where it is unset. A systemd user service or a KDE autostart inherits the variable; a launch from cron or a bare SSH session with no login session does not, and gets the exit-2 message.
  • Observed on this machine, 2026-10-28: XDG_RUNTIME_DIR=/run/user/1000, owned by the user, mode drwx------, with WAYLAND_DISPLAY=wayland-1.
  • Wayland only. No Wayland compositor, or one without the layer shell, is an environment error: tre raises UnsupportedPlatform (TRE-REQ-001 Layer Surface Window > Delivered (tre #195 closed)) and the shell exits with code 2 naming it. X11 is not supported (V1 Scope > Target environment).

Start Sequence

flowchart TD
    a[1 Pre-flight: Python, tre, Wayland] --> b[2 Find the config directory]
    b --> c[3 Read settings.tdl]
    c --> d[4 Start logging]
    d --> e[5 Take the instance lock]
    e --> f[6 Open the registry]
    f --> g[7 Discover, parse, validate files]
    g --> h[8 Resolve references and compile TEL]
    h --> i[9 Start the loop with keep_alive]
    i --> j[10 Read outputs, build the model]
    j --> k[11 Open surfaces]
    k --> l[12 Start modules]
    l --> m[Running]
Step What happens Source If it fails
1. Pre-flight Python 3.14 or later; tre 0.6.0 or later (tesserae-engine, import tess_re; the Frame needs hit_shape, from 0.5.9; the binder needs Window.build, from 0.5.10); a Wayland session; a valid XDG_RUNTIME_DIR (above) Frame > Failure modes and recovery, Surface Types Stop, code 2, a message naming the missing version or the environment
2. Config directory $XDG_CONFIG_HOME/tesserae-shell/ if set, non-empty and absolute, else $HOME/.config/tesserae-shell/ Settings File > The config directory If it does not exist, create it with the default folders (decided 2026-10-28)
3. Settings Read settings.tdl; expand ~ and variables; check paths Settings File Stop, code 1, error on the console and in the default log directory
4. Logging Start Loguru with the Logging keys, or the defaults Settings File > Log files Fall back to the console only and say so; do not stop
5. Lock Take the instance lock This note Another shell runs: stop, code 4
6. Registry Open registry.json at the configured path; create an empty one if missing; rebuild records from file UIDs when needed Config Discovery and Registry A registry that cannot be read or written: stop, code 1
7. Discover, parse, validate Find files, parse TDL (the metadata block, then the root block, which must agree with the suffix), check the schema and the semantic rules, follow Import: Validation and Errors Collect every error, then stop, code 1
8. Resolve and compile Resolve name references through the registry; compile TEL expressions ADR-0008 Tesserae Expression Language Stop, code 1
9. Loop App.run(keep_alive=True) starts the loop with zero windows; output events and call_soon work TRE-REQ-005 Keep-Alive and Loop Stop tre older than 0.5.9: caught in step 1
10. Model Read App.outputs(), match Monitor blocks by Connector, then Description, then Index (TDL Language > Monitors (Shell Schema)), build the shell model V1 Scope > Multi-monitor, Data Flow > Monitor hot-plug A Monitor block that matches no monitor is ignored
11. Surfaces Create one window per surface per monitor, from a callback on the loop Surfaces A surface that fails (a bad layer, tre failed) is logged and skipped; the others open
12. Modules Create each module class once and call start() Module Contract > Lifecycle A module whose start raises is marked failed and logged; the others continue

Running

  • No windows: stay idle (decided 2026-10-26). If every monitor is set to none, or no output is present, the shell keeps running and waits for output_added (Data Flow > Monitor hot-plug).
  • Monitors come and go: windows close with output_removed and are created again when the output returns; a dead window is never reused.
  • An unhandled exception in a callback is logged by tre and does not stop the loop (Process Model and Threading > What tre guarantees).
  • The shell reports a ready state once step 12 finishes (a log line, a State property change on the shell's D-Bus interface, and a notice to systemd, below).

Telling Systemd (Decided 2026-10-28)

The shell notifies systemd. When the environment variable NOTIFY_SOCKET is set, the shell sends one datagram with the text READY=1 to that socket once step 12 finishes, and STOPPING=1 when the stop sequence starts. systemd acts on them only when the unit uses Type=notify (sd_notify, read 2026-10-28). The protocol is a single datagram on a Unix socket, so the shell needs no library for it. With no NOTIFY_SOCKET set, the shell sends nothing. The unit file that sets Type=notify is a delivery item (70 Delivery, to write).

Stop Sequence

Triggers: SIGTERM or SIGINT, a Quit call on the shell's D-Bus interface, the session ending. SIGHUP reloads (proposed, Hot Reload). Python signal handlers do not run on an idle loop, so a watcher thread reads a socketpair fed by signal.set_wakeup_fd and calls LoopHandle.stop() (TRE-REQ-005 Keep-Alive and Loop Stop, Process Model and Threading > Lifecycle (proposed)).

  1. Set the state to Stopping; send STOPPING=1; ignore file changes and reload requests.
  2. LoopHandle.stop(): every window closes without a veto (each hears closed, reason "stopped"), and App.run() returns.
  3. Stop modules in reverse start order: call stop(), join the module's threads. A module gets 3 seconds; the whole step gets 10 seconds (decided 2026-10-28; tuned in testing).
  4. Set the shell's shortcut actions inactive and drop the Tray module's host name, on the D-Bus service thread, with a limit of 1 second (Global Shortcuts > Cleaning Up, Plasma Coexistence). Then stop the D-Bus service thread, the watcher and the watchdog.
  5. Write the registry once, atomically (Config Discovery and Registry > The registry file).
  6. Flush logs, release the instance lock, exit with code 0.

  7. A second SIGTERM or SIGINT during stopping ends the process at once with code 130 after flushing logs.

  8. A step that overruns its timeout is logged by name (the module and the thread) and skipped; the process still exits.
  9. Floating Panel positions are saved when the Panel's moved event fires, not only at stop, so a killed shell loses nothing (Panel > Position and attachment).

Exit Codes

Code Meaning
0 Normal stop
1 Configuration error: settings, a file, the registry
2 Environment error: no Wayland or layer shell, tre or Python too old
4 Another shell is already running
70 An internal error: an unhandled exception outside a callback
130 Forced stop by a second signal

The codes are decided (2026-10-28). Codes 3 and 5 to 69 are left free for later use.

Configuration

Start and stop read settings.tdl keys only: Directories, Registry, Logging, Validation, Main_Shell (Settings File). Proposed additions, to settle while building: Stop_Timeout (default 10 seconds) and Module_Stop_Timeout (default 3 seconds). No other key.

Python Contract

None. A module sees start() after the model exists and stop() at shutdown (Module Contract > Lifecycle). The shell never calls a module before step 12.

Integration

Design

None. Start and stop have no visual design; a failed start prints to the console and the log, and a running shell shows its errors the way Validation and Errors says.

Accessibility

A failure that stops the shell must also be readable without a console: the shell may be launched from a session with none. Decided 2026-10-28: the same error is sent as a desktop notification through org.freedesktop.Notifications when one is available (the shell calls it as a client; it does not own the server, ADR-0004 Plasma Coexistence). Open question 3.

Performance Budget

  • Start is dominated by file parsing, validation and window creation. Validation is in code and loads no JSON Schema library (ADR-0020 Validation in Code).
  • Budgets (time to first surface, time to ready, stop time) are set in 60 Quality/ and measured on this machine; none is measured yet.
  • Stop is bounded: 10 seconds in total (decided).

Failure Modes and Recovery

Failure Result
A step before the loop fails The shell prints every error found at that step, exits with the code above, and opens no surface
A module raises in start That module is marked failed and logged; surfaces keep default values; other modules run
A surface fails to open Logged and skipped; the rest open
The Wayland connection is lost tre's windows close, the loop ends, the process exits; a supervisor restarts it
The loop hangs The watchdog logs the stall; only a supervisor can recover (Process Model and Threading > Failure and recovery)
The process is killed Nothing is written at stop; the lock is released by the kernel when the process dies, because it is a file lock (decided with the lock)
A stale lock with no process A file lock held by flock dies with the process, so there is none to clear (decided with the lock)

Open Questions

All seven were answered on 2026-10-28.

  1. One instance: a lock; a second start is refused.
  2. Exit codes: the table above.
  3. Failure notice: a desktop notification when the shell stops at start or fails a reload, opt-out.
  4. Config directory creation: create it with the default folders when missing.
  5. Timeouts: 3 seconds per module and 10 seconds in total.
  6. Ready notice: the shell notifies systemd (READY=1 and STOPPING=1 on NOTIFY_SOCKET).
  7. Lock path: $XDG_RUNTIME_DIR/tesserae-shell/, not the config directory.
  8. XDG_RUNTIME_DIR is required, with no fallback; the shell checks it in pre-flight and sets the sticky bit on its lock file (maintainer, 2026-10-28, as recommended; the control socket was dropped by ADR-0013).

Questions After Testing

Answered by: a test on the target session that lists the lock directory after a cleaner pass and checks the sticky bit.

  1. Does any cleaner run on the target session, and is the sticky bit honoured on the lock files? Context: a cleaner that removes the lock file would allow a second instance. Recommendation: keep the sticky bit and verify on the real session. #needs-verification

Acceptance Criteria

  • With no settings.tdl, the shell starts from the default folders and the surfaces open.
  • With an invalid settings.tdl, the shell exits with code 1, prints the error with file:line:column and writes it to logs/, and opens no surface.
  • With tre older than 0.5.9, the shell exits with code 2 and names the required version.
  • With XDG_RUNTIME_DIR unset, empty or relative, the shell exits with code 2 and names the variable; with a directory owned by another user it exits with code 2; with a mode other than 0700 it warns and starts.
  • The lock file sits in $XDG_RUNTIME_DIR/tesserae-shell/ (mode 0700) with the sticky bit set.
  • A second start while one runs exits with code 4 and names the first's process id.
  • With every output removed, the shell keeps running and reopens surfaces when an output returns.
  • SIGTERM on an idle shell stops it in under 10 seconds with code 0, after each module's stop ran and the registry was written.
  • A module whose stop hangs delays the stop by at most 3 seconds, is named in the log, and the shell still exits.
  • A second SIGINT during stop ends the process with code 130.
  • A module whose start raises does not prevent the others from starting.
  • With NOTIFY_SOCKET set, the shell sends READY=1 after the last module starts and STOPPING=1 when it begins to stop; with it unset, nothing is sent.