Skip to content

Commit ae0ee9b

Browse files
committed
fix(desktop): an AppImage launch drops earlier launches' mount dirs from the server PATH
Starting the AppImage from a shell or agent that inherited an earlier launch's environment kept that launch's /tmp/.mount_* dirs on PATH, LD_LIBRARY_PATH and XDG_DATA_DIRS. Once one of those mounts died, every git spawn from the server failed with ENOTCONN. Scrub every segment under a .mount_* dir other than the current APPDIR right after the shell PATH merge, before the server is spawned. The server, app.relaunch and the updater all inherit process.env, so one scrub covers all three.
1 parent 511d20c commit ae0ee9b

3 files changed

Lines changed: 145 additions & 0 deletions

File tree

‎apps/desktop/src/shell/DesktopShellEnvironment.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as ChildProcess from "effect/process/ChildProcess";
1111
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1212

1313
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
14+
import { installStaleAppImageMountScrub } from "./appImageStaleMounts.ts";
1415

1516
type EnvironmentPatch = Record<string, string>;
1617

@@ -424,6 +425,9 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
424425
if (Option.isSome(mergedPath)) {
425426
config.env.PATH = mergedPath.value;
426427
}
428+
// The server, relaunch and updater all inherit process.env, so scrubbing
429+
// here keeps earlier AppImage launches' mounts out of every one of them.
430+
installStaleAppImageMountScrub(config.env);
427431
if (!config.env.SSH_AUTH_SOCK && shellEnvironment.SSH_AUTH_SOCK) {
428432
config.env.SSH_AUTH_SOCK = shellEnvironment.SSH_AUTH_SOCK;
429433
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { installStaleAppImageMountScrub, stripStaleAppImageMounts } from "./appImageStaleMounts.ts";
4+
5+
const CURRENT = "/tmp/.mount_cur";
6+
7+
describe("stripStaleAppImageMounts", () => {
8+
it("drops earlier launches' mount segments and keeps the current APPDIR's", () => {
9+
const result = stripStaleAppImageMounts({
10+
APPIMAGE: "/home/me/T3-Code.AppImage",
11+
APPDIR: CURRENT,
12+
PATH: `${CURRENT}/usr/bin:/tmp/.mount_abc/usr/bin:/usr/local/bin:/tmp/.mount_def/usr/sbin:/usr/bin`,
13+
LD_LIBRARY_PATH: `/tmp/.mount_abc/usr/lib:${CURRENT}/usr/lib:/opt/lib`,
14+
XDG_DATA_DIRS: `/tmp/.mount_abc/usr/share:/usr/share:${CURRENT}/usr/share`,
15+
HOME: "/home/me",
16+
});
17+
18+
expect(result.PATH).toBe(`${CURRENT}/usr/bin:/usr/local/bin:/usr/bin`);
19+
expect(result.LD_LIBRARY_PATH).toBe(`${CURRENT}/usr/lib:/opt/lib`);
20+
expect(result.XDG_DATA_DIRS).toBe(`/usr/share:${CURRENT}/usr/share`);
21+
expect(result.HOME).toBe("/home/me");
22+
});
23+
24+
it("treats a trailing slash on APPDIR and the bare mount root as the current mount", () => {
25+
const result = stripStaleAppImageMounts({
26+
APPDIR: `${CURRENT}/`,
27+
PATH: `${CURRENT}:/tmp/.mount_abc:/tmp/.mount_cur2/usr/bin:/usr/bin`,
28+
});
29+
30+
expect(result.PATH).toBe(`${CURRENT}:/usr/bin`);
31+
});
32+
33+
it("removes every mount segment when APPDIR is missing", () => {
34+
const result = stripStaleAppImageMounts({
35+
APPIMAGE: "/home/me/T3-Code.AppImage",
36+
PATH: "/tmp/.mount_abc/usr/bin:/usr/bin",
37+
LD_LIBRARY_PATH: "/tmp/.mount_abc/usr/lib",
38+
});
39+
40+
expect(result.PATH).toBe("/usr/bin");
41+
expect(result).not.toHaveProperty("LD_LIBRARY_PATH");
42+
});
43+
44+
it("matches mounts under a non-default TMPDIR", () => {
45+
const result = stripStaleAppImageMounts({
46+
APPDIR: "/var/tmp/.mount_cur",
47+
PATH: "/var/tmp/.mount_old/usr/bin:/var/tmp/.mount_cur/usr/bin:/usr/bin",
48+
});
49+
50+
expect(result.PATH).toBe("/var/tmp/.mount_cur/usr/bin:/usr/bin");
51+
});
52+
53+
it("preserves empty segments, order and absent variables", () => {
54+
const result = stripStaleAppImageMounts({
55+
APPDIR: CURRENT,
56+
PATH: "/usr/bin::/tmp/.mount_abc/usr/bin:/bin:",
57+
XDG_DATA_DIRS: "",
58+
});
59+
60+
expect(result.PATH).toBe("/usr/bin::/bin:");
61+
expect(result.XDG_DATA_DIRS).toBe("");
62+
expect(result).not.toHaveProperty("LD_LIBRARY_PATH");
63+
});
64+
65+
it("leaves a non-AppImage env untouched", () => {
66+
const env = {
67+
PATH: "/tmp/.mount_abc/usr/bin:/usr/bin",
68+
LD_LIBRARY_PATH: "/tmp/.mount_abc/usr/lib",
69+
};
70+
71+
expect(stripStaleAppImageMounts(env)).toBe(env);
72+
expect(env.PATH).toBe("/tmp/.mount_abc/usr/bin:/usr/bin");
73+
});
74+
});
75+
76+
describe("installStaleAppImageMountScrub", () => {
77+
it("rewrites the env in place and deletes variables left with only stale mounts", () => {
78+
const env: NodeJS.ProcessEnv = {
79+
APPDIR: CURRENT,
80+
PATH: `/tmp/.mount_abc/usr/bin:${CURRENT}/usr/bin:/usr/bin`,
81+
LD_LIBRARY_PATH: "/tmp/.mount_abc/usr/lib",
82+
XDG_DATA_DIRS: "/usr/share",
83+
};
84+
85+
installStaleAppImageMountScrub(env);
86+
87+
expect(env).toEqual({
88+
APPDIR: CURRENT,
89+
PATH: `${CURRENT}/usr/bin:/usr/bin`,
90+
XDG_DATA_DIRS: "/usr/share",
91+
});
92+
});
93+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Search-path variables the AppImage runtime prepends its mount dir to.
2+
const APPIMAGE_SEARCH_PATH_ENV_KEYS = ["PATH", "LD_LIBRARY_PATH", "XDG_DATA_DIRS"] as const;
3+
4+
// The AppImage runtime mounts the app at `$TMPDIR/.mount_<id>`. Captures that
5+
// mount root for any segment inside it.
6+
const APPIMAGE_MOUNT_ROOT = /^(.*?\/\.mount_[^/]+)(?:\/|$)/;
7+
8+
/**
9+
* Removes search-path segments that point into another AppImage launch's
10+
* `.mount_*` dir. Launching T3 Code from a terminal or agent that inherited an
11+
* earlier launch's env stacks every older mount onto PATH; once one of them is
12+
* unmounted, every spawn that walks PATH (git, shells) fails with ENOTCONN.
13+
* Segments under the current APPDIR stay, as do all non-mount segments.
14+
* Non-AppImage envs are returned as is.
15+
*
16+
* Can fold into the shared `appImageRuntimeEnv` helper once #16679 lands.
17+
*/
18+
export function stripStaleAppImageMounts(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
19+
if (env.APPIMAGE === undefined && env.APPDIR === undefined) return env;
20+
21+
const appDir = env.APPDIR?.replace(/\/+$/, "");
22+
const scrubbed: NodeJS.ProcessEnv = { ...env };
23+
for (const key of APPIMAGE_SEARCH_PATH_ENV_KEYS) {
24+
const value = env[key];
25+
if (value === undefined) continue;
26+
const kept = value.split(":").filter((segment) => {
27+
const mountRoot = APPIMAGE_MOUNT_ROOT.exec(segment)?.[1];
28+
return mountRoot === undefined || mountRoot === appDir;
29+
});
30+
if (kept.some((segment) => segment.length > 0)) {
31+
scrubbed[key] = kept.join(":");
32+
} else if (value.length > 0) {
33+
delete scrubbed[key];
34+
}
35+
}
36+
return scrubbed;
37+
}
38+
39+
/** Applies {@link stripStaleAppImageMounts} to `env` in place. */
40+
export function installStaleAppImageMountScrub(env: NodeJS.ProcessEnv): void {
41+
const scrubbed = stripStaleAppImageMounts(env);
42+
if (scrubbed === env) return;
43+
for (const key of APPIMAGE_SEARCH_PATH_ENV_KEYS) {
44+
const value = scrubbed[key];
45+
if (value === undefined) delete env[key];
46+
else env[key] = value;
47+
}
48+
}

0 commit comments

Comments
 (0)