Repository navigation
fix(server): connects no longer wait seconds on a failing tailscale status #26794
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
juliusmarminge
wants to merge
6
commits into
main
Choose a base branch
from
fix/issue-26775
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.
+521
−42
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
271fb8c
test(server): reproduce #26775, connects re-read a failing tailscale …
juliusmarminge bceee1a
fix(server): connects share one tailscale status read instead of wait…
juliusmarminge 447455d
fix(server): open config subscriptions get Tailscale targets that rec…
juliusmarminge fdf8e89
fix(server): connects wait briefly for a fresh Tailscale read, change…
juliusmarminge c526075
fix(server): a refresh claim and its fork cannot be split by an inter…
juliusmarminge 4ecd25f
Merge remote-tracking branch 'origin/main' into fix/issue-26775
juliusmarminge 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
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,95 @@ | ||
| import { | ||
| readTailscaleStatus, | ||
| type TailscaleCommandError, | ||
| type TailscaleStatus, | ||
| type TailscaleStatusParseError, | ||
| } from "@t3tools/tailscale"; | ||
| import * as Context from "effect/Context"; | ||
| import * as Deferred from "effect/Deferred"; | ||
| import * as Duration from "effect/Duration"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Exit from "effect/Exit"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import * as PubSub from "effect/PubSub"; | ||
| import type * as Scope from "effect/Scope"; | ||
| import * as Stream from "effect/Stream"; | ||
| import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; | ||
|
|
||
| type TailscaleStatusError = TailscaleCommandError | TailscaleStatusParseError; | ||
|
|
||
| export class TailscaleStatusReader extends Context.Service< | ||
| TailscaleStatusReader, | ||
| { | ||
| readonly read: () => Effect.Effect<TailscaleStatus, TailscaleStatusError>; | ||
| /** Emits when a read settles to a different outcome than the previous settled read. */ | ||
| readonly subscribeChanges: Effect.Effect<Stream.Stream<void>, never, Scope.Scope>; | ||
| } | ||
| >()("t3/environment/TailscaleStatusReader") {} | ||
|
|
||
| /** Answer from the last settled read without refreshing, for re-resolves that a settled change drives. */ | ||
| export const SettledReadOnly = Context.Reference<boolean>( | ||
| "t3/environment/TailscaleStatusReader/SettledReadOnly", | ||
| { defaultValue: () => false }, | ||
| ); | ||
|
|
||
| // A healthy CLI answers in tens of milliseconds, so a connect still gets a fresh read from it. | ||
| const FRESH_READ_GRACE = Duration.millis(250); | ||
|
|
||
| // Callers treat every failure alike, so failures compare equal regardless of cause. | ||
| const outcomeKey = (exit: Exit.Exit<TailscaleStatus, TailscaleStatusError>) => | ||
| Exit.isSuccess(exit) ? JSON.stringify(exit.value) : null; | ||
|
|
||
| const make = Effect.gen(function* () { | ||
| const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; | ||
| // The layer's scope, so a caller that times out or disconnects never interrupts the shared read. | ||
| const scope = yield* Effect.scope; | ||
| const changes = yield* PubSub.unbounded<void>(); | ||
| let settled: Exit.Exit<TailscaleStatus, TailscaleStatusError> | undefined; | ||
| let inFlight: Deferred.Deferred<TailscaleStatus, TailscaleStatusError> | undefined; | ||
|
|
||
| const refresh = Effect.suspend(() => { | ||
| if (inFlight !== undefined) return Effect.succeed(inFlight); | ||
| const deferred = Deferred.makeUnsafe<TailscaleStatus, TailscaleStatusError>(); | ||
| inFlight = deferred; | ||
| return readTailscaleStatus.pipe( | ||
| Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), | ||
| Effect.onExit((exit) => | ||
| Effect.suspend(() => { | ||
| const changed = settled === undefined || outcomeKey(settled) !== outcomeKey(exit); | ||
| settled = exit; | ||
| inFlight = undefined; | ||
| return Deferred.done(deferred, exit).pipe( | ||
| Effect.andThen(changed ? PubSub.publish(changes, undefined) : Effect.void), | ||
| ); | ||
| }), | ||
| ), | ||
| Effect.interruptible, | ||
| Effect.forkIn(scope), | ||
| Effect.as(deferred), | ||
| ); | ||
| }).pipe( | ||
| // Claiming `inFlight` and forking are one step: an interrupt between them would strand the claim. | ||
| Effect.uninterruptible, | ||
| ); | ||
| yield* refresh; | ||
|
|
||
| return TailscaleStatusReader.of({ | ||
| // A caller gets the refresh it starts or joins if that settles within the grace, else the | ||
| // previous read's result; a later outcome reaches open subscribers through `subscribeChanges`. | ||
| read: Effect.fn("TailscaleStatusReader.read")(function* () { | ||
| const previous = settled; | ||
| if (previous !== undefined && (yield* SettledReadOnly)) return yield* previous; | ||
| const pending = yield* refresh; | ||
| if (previous === undefined) return yield* Deferred.await(pending); | ||
| const fresh = yield* Deferred.await(pending).pipe( | ||
| Effect.exit, | ||
| Effect.timeoutOption(FRESH_READ_GRACE), | ||
| ); | ||
| return yield* Option.getOrElse(fresh, () => previous); | ||
| }), | ||
| subscribeChanges: PubSub.subscribe(changes).pipe(Effect.map(Stream.fromSubscription)), | ||
| }); | ||
| }); | ||
|
|
||
| export const layer = Layer.effect(TailscaleStatusReader, make); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: pingdotgg/t3code
Length of output: 4531
Script executed:
Repository: pingdotgg/t3code
Length of output: 11463
Script executed:
Repository: pingdotgg/t3code
Length of output: 3784
Script executed:
Repository: pingdotgg/t3code
Length of output: 11251
Script executed:
Repository: pingdotgg/t3code
Length of output: 7685
Script executed:
Repository: pingdotgg/t3code
Length of output: 2378
Bound the first
TailscaleStatusReader.readwait to 250 ms.subscribeServerConfigwaits forresolveNetworkTargets. On the first read,TailscaleStatusReader.readawaits the pending CLI result withoutFRESH_READ_GRACE. A stalledtailscale status --jsoncan therefore delay the initial snapshot for its 3.5-second CLI timeout, plus cleanup time.Apply the 250 ms bound only to the caller’s wait. Keep the shared refresh running so its later result still updates
settledand publishes throughsubscribeChanges. Both network resolvers already convert a read failure into an empty discovery result.