Requests on a signed-out account's WordPressComRestApi crash the app
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- swift
- 領域
- api, networking
調査の方向性
Start with WordPressComRestApi.perform in WordPressComRestApi.swift and the task-creation path in HTTPClient.swift; trace how urlSession and uploadURLSession requests are created and invalidated. Then add the described sign-out/request unit test, taking care to prevent the account save from refaulting it. Done means a held API object returns an error rather than crashing after sign-out, and a slow-request sign-out does not crash or show an error.
索引モデルが issue の本文から書いたものです。
説明
Making a request on a signed-out account's WordPressComRestApi terminates the app. Each caller has to avoid this on its own, and new callers keep reintroducing it, so the fix belongs in WordPressComRestApi.
What happens
The app crashes with:
NSGenericException: Task created in a session that has been invalidated
Who is affected
Anyone who signs out while a feature still holds that account's API object and is about to make a request. The window is usually small, and a slow network makes it longer.
Root cause
Sign-out deletes the WPAccount, and WPAccount.prepareForDeletion calls invalidateAndCancelTasks() on the cached WordPressComRestApi. That invalidates its URLSessions for good, but the API object still accepts requests. The next request calls dataTask(with:) on the invalidated session, which raises an Objective-C exception. Swift can't catch it.
Known paths
- #25793 (open): Reader mention lookups. Confirmed in the Jetpack app on an iOS 27.0 simulator: signing out during the lookups crashed with the stack below, in 1 of 1 valid runs, with lookups delayed 6 s each by a local proxy. That PR needs its own fix regardless of this issue.
- #26136: the Notifications bell activity service. A guard is proposed there for the same reason.
NotificationSyncMediator.sync: crashes if sign-out lands between the hashes response and theloadNotescall. Reproduced in the unit-test host, not in the app.
The crash doesn't always happen. Refaulting the main-context WPAccount drops its cached API, so an object handed out earlier is never invalidated. Its requests then succeed with the removed account's token. That is a separate problem, and this issue doesn't fix it.
Suggested fix
Have WordPressComRestApi remember that it was invalidated, and return an error instead of creating a task:
private let isInvalidated = OSAllocatedUnfairLock(initialState: false)
@objc open func invalidateAndCancelTasks() {
isInvalidated.withLock { $0 = true }
for session in [urlSession, uploadURLSession] {
session.invalidateAndCancel()
}
}
The internal perform that both urlSession and uploadURLSession go through would then check first:
guard !isInvalidated.withLock({ $0 }) else {
return .failure(.connection(URLError(.cancelled)))
}
This sketch has not been compiled or run.
Open questions
- Remaining gap: sign-out can still land between that check and task creation. Closing it fully means holding the lock around task creation in
HTTPClient. - Other request paths: any code that uses these sessions without going through
performneeds the same check. - Error handling: check that callers treat
.cancelledas a quiet failure, with no error shown to someone who has just signed out.
Test plan
- Add a unit test that signs out, then makes a request on the held API object, and expects an error rather than a crash. Spin the run loop after creating the account, or its save refaults the account and the test passes without exercising the crash.
- Sign out during a slow request in the app and confirm there is no crash.
Stack from #25793 (Jetpack Debug build, head f6d731aff9)
CFNetwork
NSURLSession.task(for:completion:) (HTTPClient.swift:195)
closure #1 in closure #1 in NSURLSession.perform<A>(request:acceptableStatusCodes:taskCreated:fulfilling:errorType:)
NSURLSession.perform<A>(request:acceptableStatusCodes:taskCreated:fulfilling:errorType:)
WordPressComRestApi.perform<A>(request:fulfilling:decoder:taskCreated:session:)
WordPressComRestApi.perform<A>(_:URLString:parameters:fulfilling:decoder:) (WordPressComRestApi.swift:425)
WordPressComRestApi.perform(_:URLString:parameters:fulfilling:) (WordPressComRestApi.swift:383)
WordPressComReaderUserProfileService.fetchProfile(handle:) (ReaderUserProfileService.swift:39)
ReaderDetailCoordinator.resolveMentionProfiles(for:) (ReaderDetailCoordinator.swift:235)
closure #1 in ReaderDetailViewController.render(_:) (ReaderDetailViewController.swift:272)
Line numbers in the Reader files are from a build with temporary logging added.
- 主要言語
- Swift
- スター
- 3.9k
- フォーク
- 1.2k
- 平均マージ
- 1日 23時間
- マージ済み PR(30日)
- 136
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
wordpress-mobile/WordPress-iOS のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
wordpress-mobile/WordPress-iOS#26423 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Pri] Low [Status] Ready for Dev [Type] Bug /Me
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
wordpress-mobile/WordPress-iOS#26279 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Pri] Low [Status] Ready for Dev [Type] Bug Share Extension
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
wordpress-mobile/WordPress-iOS#26276 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Pri] Low [Status] Ready for Dev [Type] Bug Blogging Prompts Notifications
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
wordpress-mobile/WordPress-iOS#26256 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Status] Ready for Dev [Type] Bug Reader
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
wordpress-mobile/WordPress-iOS#26254 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
wordpress-mobile/WordPress-iOS の issue をすべて見る
似ている issue
-
Optional callbacks log "Unable to create JSCallback" when passed null対応中かも @cmsj が今日担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
cmsj/Hammerspoon2#464 ·
-
android bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
VocaHQ/vocaphone#470 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
new-compilers request
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
compiler-explorer/compiler-explorer#9640 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100