Repository navigation
fix: detect service-managed local gateways on Linux #984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JaimeMarques
wants to merge
2
commits into
fathah:main
Choose a base branch
from
JaimeMarques:fix/local-multiplex-gateway-status
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { readFileSync, realpathSync } from "fs"; | ||
| import { join } from "path"; | ||
|
|
||
| /** Match the default home's managed gateway, not a reused PID or a named profile's missing API port. */ | ||
| export function isMultiplexGatewayRunning( | ||
| home: string, | ||
| profile: string, | ||
| isGatewayPidAlive: (pid: number) => boolean, | ||
| ): boolean { | ||
| // Named profiles in a multiplexer have no dedicated Local API listener yet. | ||
| if (profile !== "default" || process.platform !== "linux") return false; | ||
| try { | ||
| const state = JSON.parse( | ||
| readFileSync(join(home, "gateway_state.json"), "utf-8"), | ||
| ) as { | ||
| kind?: unknown; | ||
| hermes_home?: unknown; | ||
| gateway_state?: unknown; | ||
| pid?: unknown; | ||
| start_time?: unknown; | ||
| served_profiles?: unknown; | ||
| }; | ||
| if ( | ||
| state.kind !== "hermes-gateway" || | ||
| typeof state.hermes_home !== "string" || | ||
| realpathSync(state.hermes_home) !== realpathSync(home) || | ||
| state.gateway_state !== "running" || | ||
| typeof state.pid !== "number" || | ||
| !Number.isSafeInteger(state.pid) || | ||
| state.pid <= 0 || | ||
| typeof state.start_time !== "number" || | ||
| !Number.isSafeInteger(state.start_time) || | ||
| !Array.isArray(state.served_profiles) || | ||
| !state.served_profiles.includes("default") | ||
| ) { | ||
| return false; | ||
| } | ||
| // Hermes Agent records Linux /proc field 22 (ticks since boot). Match | ||
| // that fingerprint so a dead gateway's PID reused by another process | ||
| // cannot make a stale state record appear online. | ||
| const stat = readFileSync(`/proc/${state.pid}/stat`, "utf-8"); | ||
| const closingParen = stat.lastIndexOf(")"); | ||
| if (closingParen < 0) return false; | ||
| const startTime = Number( | ||
| stat | ||
| .slice(closingParen + 2) | ||
| .trim() | ||
| .split(/\s+/)[19], | ||
| ); | ||
| return startTime === state.start_time && isGatewayPidAlive(state.pid); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| getConfigValue: vi.fn(), | ||
| setConfigValue: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("./config", () => mocks); | ||
| vi.mock("./installer", () => ({ HERMES_HOME: "/unused-hermes-home" })); | ||
| vi.mock("./utils", () => ({ | ||
| normalizeProfileName: (profile?: string) => | ||
| profile === "default" ? undefined : profile, | ||
| })); | ||
|
|
||
| import { getProfilePort } from "./gateway-ports"; | ||
|
|
||
| describe("default Local API port", () => { | ||
| // @lat: [[connections#Test specifications#Default API port selection]] | ||
| it("uses its configured port without changing config", () => { | ||
| mocks.getConfigValue.mockReturnValue("18765"); | ||
| expect(getProfilePort("default")).toBe(18765); | ||
| expect(mocks.setConfigValue).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.