Repository navigation
fix(mobile): do not call a suspended dial an unreachable host #24425
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
innocarpe
wants to merge
3
commits into
stablyai:main
Choose a base branch
from
innocarpe:fix-18829-suspended-dial-timeout
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
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { | ||
| releaseSocketTimer, | ||
| rememberSocketDeadlineClock, | ||
| startSocketDeadlineClock, | ||
| takeSocketDeadline | ||
| } from './socket-deadline-clock' | ||
| import { describeSocketDeadline } from './socket-deadline-suspension' | ||
|
|
||
| const appState = vi.hoisted(() => ({ | ||
| currentState: 'active', | ||
| listeners: new Set<(state: string) => void>() | ||
| })) | ||
|
|
||
| vi.mock('react-native', () => ({ | ||
| AppState: { | ||
| get currentState() { | ||
| return appState.currentState | ||
| }, | ||
| addEventListener: (_event: string, listener: (state: string) => void) => { | ||
| appState.listeners.add(listener) | ||
| return { | ||
| remove: () => { | ||
| appState.listeners.delete(listener) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| })) | ||
|
|
||
| describe('startSocketDeadlineClock', () => { | ||
| it('reports a wall-clock jump while the app stays active as a host timeout', () => { | ||
| appState.currentState = 'active' | ||
| appState.listeners.clear() | ||
| let wallMs = 1_000 | ||
| let monotonicMs = 50 | ||
| const clock = startSocketDeadlineClock( | ||
| () => wallMs, | ||
| () => monotonicMs | ||
| ) | ||
| wallMs += 3 * 60 * 60 * 1000 | ||
| monotonicMs += 12_000 | ||
| const report = describeSocketDeadline({ | ||
| kind: 'connect', | ||
| timeoutMs: 12_000, | ||
| ...clock.finish() | ||
| }) | ||
|
|
||
| expect(report.code).toBe('connect-timeout') | ||
| expect(appState.listeners.size).toBe(0) | ||
| }) | ||
|
|
||
| it('keeps a host timeout when a short foreground loss does not delay the clocks', () => { | ||
| appState.currentState = 'active' | ||
| appState.listeners.clear() | ||
| let wallMs = 50_000 | ||
| let monotonicMs = 10 | ||
| const clock = startSocketDeadlineClock( | ||
| () => wallMs, | ||
| () => monotonicMs | ||
| ) | ||
| for (const listener of appState.listeners) { | ||
| listener('background') | ||
| } | ||
| wallMs -= 5_000 | ||
| monotonicMs += 12_000 | ||
| const report = describeSocketDeadline({ | ||
| kind: 'connect', | ||
| timeoutMs: 12_000, | ||
| ...clock.finish() | ||
| }) | ||
|
|
||
| expect(report.code).toBe('connect-timeout') | ||
| expect(report.detail).toContain('endpoint unreachable') | ||
| }) | ||
|
|
||
| it('keeps an on-budget timeout when a transient inactive state has ended', () => { | ||
| appState.currentState = 'active' | ||
| appState.listeners.clear() | ||
| let wallMs = 1_000 | ||
| let monotonicMs = 50 | ||
| const clock = startSocketDeadlineClock( | ||
| () => wallMs, | ||
| () => monotonicMs | ||
| ) | ||
| for (const listener of appState.listeners) { | ||
| listener('inactive') | ||
| listener('active') | ||
| } | ||
| wallMs += 12_000 | ||
| monotonicMs += 12_000 | ||
|
|
||
| const report = describeSocketDeadline({ | ||
| kind: 'connect', | ||
| timeoutMs: 12_000, | ||
| ...clock.finish() | ||
| }) | ||
|
|
||
| expect(report.code).toBe('connect-timeout') | ||
| }) | ||
|
|
||
| it('detects suspension when the watcher starts after the app is already backgrounded', () => { | ||
| appState.currentState = 'background' | ||
| appState.listeners.clear() | ||
| let wallMs = 1_000 | ||
| let monotonicMs = 50 | ||
| const clock = startSocketDeadlineClock( | ||
| () => wallMs, | ||
| () => monotonicMs | ||
| ) | ||
| wallMs += 3 * 60 * 60 * 1000 | ||
| monotonicMs += 12_000 | ||
|
|
||
| const report = describeSocketDeadline({ | ||
| kind: 'handshake', | ||
| timeoutMs: 5_000, | ||
| ...clock.finish() | ||
| }) | ||
|
|
||
| expect(report.code).toBe('suspended-dial') | ||
| expect(appState.listeners.size).toBe(0) | ||
| }) | ||
|
|
||
| it('drops the foreground watch when the timer is cleared', () => { | ||
| appState.currentState = 'active' | ||
| appState.listeners.clear() | ||
| const timer = setTimeout(() => undefined, 60_000) | ||
| rememberSocketDeadlineClock(timer, startSocketDeadlineClock()) | ||
| expect(appState.listeners.size).toBe(1) | ||
|
|
||
| expect(releaseSocketTimer(timer)).toBeNull() | ||
| expect(appState.listeners.size).toBe(0) | ||
| expect(takeSocketDeadline(timer).leftForeground).toBe(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,95 @@ | ||
| import { AppState } from 'react-native' | ||
|
|
||
| export type SocketDeadlineSample = { | ||
| armedAtMs: number | ||
| firedAtMs: number | ||
| monotonicElapsedMs: number | ||
| leftForeground: boolean | ||
| } | ||
|
|
||
| type DeadlineTimer = ReturnType<typeof setTimeout> | ||
|
|
||
| type SocketDeadlineClock = { | ||
| finish: () => SocketDeadlineSample | ||
| } | ||
|
|
||
| const boundClocks = new Map<DeadlineTimer, SocketDeadlineClock>() | ||
|
|
||
| function defaultMonotonicNow(): number { | ||
| if (typeof performance !== 'undefined' && typeof performance.now === 'function') { | ||
| return performance.now() | ||
| } | ||
| return Date.now() | ||
| } | ||
|
|
||
| function watchForegroundLoss(onLeave: () => void): () => void { | ||
| const subscription = AppState.addEventListener('change', (state) => { | ||
| if (state === 'background' || state === 'inactive') { | ||
| onLeave() | ||
| } | ||
| }) | ||
| if (AppState.currentState === 'background' || AppState.currentState === 'inactive') { | ||
| onLeave() | ||
| } | ||
| return () => { | ||
| subscription.remove() | ||
| } | ||
| } | ||
|
|
||
| export function startSocketDeadlineClock( | ||
| now: () => number = Date.now, | ||
| monotonicNow: () => number = defaultMonotonicNow, | ||
| watchLeave: (onLeave: () => void) => () => void = watchForegroundLoss | ||
| ): SocketDeadlineClock { | ||
| const armedAtMs = now() | ||
| const armedMonotonicMs = monotonicNow() | ||
| let leftForeground = false | ||
| let stopped = false | ||
| const stopWatch = watchLeave(() => { | ||
| leftForeground = true | ||
| }) | ||
| return { | ||
| finish() { | ||
| if (!stopped) { | ||
| stopped = true | ||
| stopWatch() | ||
| } | ||
| return { | ||
| armedAtMs, | ||
| firedAtMs: now(), | ||
| monotonicElapsedMs: monotonicNow() - armedMonotonicMs, | ||
| leftForeground | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function rememberSocketDeadlineClock( | ||
| timer: DeadlineTimer, | ||
| clock: SocketDeadlineClock | ||
| ): void { | ||
| boundClocks.set(timer, clock) | ||
| } | ||
|
|
||
| export function takeSocketDeadline(timer: DeadlineTimer): SocketDeadlineSample { | ||
| const clock = boundClocks.get(timer) | ||
| boundClocks.delete(timer) | ||
| if (!clock) { | ||
| const firedAtMs = Date.now() | ||
| return { | ||
| armedAtMs: firedAtMs, | ||
| firedAtMs, | ||
| monotonicElapsedMs: 0, | ||
| leftForeground: false | ||
| } | ||
| } | ||
| return clock.finish() | ||
| } | ||
|
|
||
| export function releaseSocketTimer(timer: DeadlineTimer | null): null { | ||
| if (timer) { | ||
| clearTimeout(timer) | ||
| takeSocketDeadline(timer) | ||
| } | ||
| return null | ||
| } | ||
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.