Skip to content

fix(network): stop HTTP and WebSocket requests after old version error - #6897

Open
SeniorZhai wants to merge 2 commits into
masterfrom
fix/stop-requests-on-old-version
Open

SeniorZhai wants to merge 2 commits into
masterfrom
fix/stop-requests-on-old-version

Conversation

@SeniorZhai

Copy link
Copy Markdown
Member

No description provided.

@gaotic

gaotic Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

AI review done for 253a0ba in 27m 11s · 6 comments

Quality: The revision improves transaction callback safety and expands dispatcher coverage. The shutdown still risks losing persistent sends and allowing network work to continue while the update block is active, as verified through source inspection and focused probes with stubbed dependencies.

Role Model Status
Reviewer GPT codex/gpt-6.2-sol · effort high ok
Reviewer Grok grok/grok-4.7@500k · effort medium ok
Summary codex/gpt-6.2-sol · effort medium ok

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · GPT + Grok · 4e3a165

Request changes. git diff --check passed, and focused Kotlin checks reproduced the callback race and confirmed retry classification. The offline Gradle run stopped during settings configuration because the Foojay resolver plugin was unavailable, so Android assembly, lint, and unit tests did not run.

} else {
if (blazeMessage.error.code == OLD_VERSION) {
if (transactions[blazeMessage.id] != null) {
transactions[blazeMessage.id]!!.error.error(blazeMessage)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If HTTP and Blaze both return OLD_VERSION, HTTP shutdown can clear transactions between these two lookups. The !! then throws in the receive coroutine and reaches the process-wide crash handler. Remove the transaction atomically and invoke the callback on the returned value. Use the same pattern for the other response branches, which can race with the same shutdown.

}
runCatching {
getJobManager().apply {
cancelAllJob()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can orphan an outgoing message when Blaze rejects an in-flight send with OLD_VERSION. The callback releases the send job, then cancelAllJob() can mark it cancelled before deliver() throws. JobQueue drops the persistent job when retry is declined, and onCancel() leaves the message at SENDING. Pause sends for the version block and preserve their queued jobs so they can resume after an upgrade.

runCatching {
val networkRuntime = getNetworkRuntime()
networkRuntime.getChatWebSocket().disconnect()
networkRuntime.getOkHttpClient().dispatcher.cancelAll()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cancels only the primary client's dispatcher. Route, Referral, Cash, and Earn each have their own dispatcher, so a call already past its version guard can continue after shutdown, including POST web3/swap. Cancel calls on all four dispatchers as part of the same transition.

// Interceptor
builder.addInterceptor { chain ->
if (MixinApplication.get().isOldVersion()) {
throw ClientErrorException(OLD_VERSION)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseJob.shouldRetry() treats this exception as a retryable IOException, so a persistent job such as RefreshAccountJob can repeatedly hit this guard. After a process restart, JobQueue starts running when injected, while the restored version flag skips the one-time stop(). Keep the queue paused while the persisted block is active and handle this exception explicitly before the generic IOException branch.

defaultSharedPreferences.putInt(Constants.Account.PREF_OLD_VERSION_CODE, BuildConfig.VERSION_CODE)
if (firstTrigger) {
stopNetworkWorkForOldVersion()
InitializeActivity.showOldVersionAlert(this)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android can silently deny this one-time activity launch when Blaze reports OLD_VERSION in the background with no app task available. The block still persists, but the only foreground fallback is MainActivity.onCreate(). A direct entry such as the share target can open without showing the update screen. Check the block when an app activity comes to the foreground and present the alert there if needed.

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · GPT + Grok · 253a0ba

Request changes. Whitespace checks passed for the full and incremental diffs, and focused Kotlin and Java probes reproduced the retry classification, WebSocket timing gaps, signal-key loop, and upload behavior. Offline Gradle stopped at settings configuration because the Foojay resolver plugin was unavailable, so Android assembly, lint, and unit tests did not run.

if (throwable is MissingAccountScopeException) {
return false
}
if (throwable is ClientErrorException) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HTTP version guard can permanently drop a queued first message. An active SendMessageJob that reaches conversationApi.create() after the block is set gets ClientErrorException(OLD_VERSION). This branch turns it into RetryConstraint.CANCEL, and onCancel() leaves the local row at SENDING. Treat OLD_VERSION as a deferral that preserves the persistent job until upgrade.

runCatching {
val networkRuntime = getNetworkRuntime()
networkRuntime.getChatWebSocket().disconnect()
networkRuntime.getOkHttpClient().dispatcher.cancelAll()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cancelling the main dispatcher during requestAttachment() loses the pending upload. Both attachment jobs catch the resulting exception, mark the media CANCELED, and return normally, so JobQueue deletes the job. Route version-shutdown failures through a retryable deferral before the terminal media-status update.

networkRuntime.getNetworkDispatcherRegistry().cancelAll()
}
runCatching {
getJobManager().stop()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An attachment that already has its upload URL can keep uploading after OLD_VERSION. stop() leaves active jobs running, and Util.uploadAttachment() uses a separate OkHttp client whose request body only checks the job's cancellation flag. Cancel these transfers on the version block and preserve their jobs for retry after upgrade.

jobManager.addJobInBackground(RefreshOffsetJob())
sendPendingMessage()
if (!MixinApplication.get().isOldVersion()) {
jobManager.start()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An HTTP OLD_VERSION response can stop JobQueue between the preceding version check and this start(). A late onOpen() then restarts the queue while the persisted block is active, and the one-time shutdown has already run. Serialize the queue start and version shutdown so the block always wins.

BlazeMessageService.stopService(this)
runCatching {
val networkRuntime = getNetworkRuntime()
networkRuntime.getChatWebSocket().disconnect()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disconnect() waits on the monitor held by sendMessage(). If HTTP sets the version block while a sender is serializing its frame, the sender can still enqueue it on the live socket because it only checks client and connected. Coordinate the version block with frame admission and abort the socket without waiting for a sender's response timeout.

blazeMessage.params?.conversation_id?.let(onChecksumInvalid)
throw ChecksumException()
}
OLD_VERSION -> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encrypted sends can get stuck before this branch. JobSenderKey.signalKeysChannel() retries the OLD_VERSION response and then retries each null from the disconnected socket, holding a JobQueue worker indefinitely. Exit the key lookup with a retryable deferral on OLD_VERSION or an active version block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant