Skip to content

[Bug]: Quit hangs in ThreadPool shutdown on a busy Mac: the preload code cache write runs BEST_EFFORT + SKIP_ON_SHUTDOWN on a background-QoS thread #56033

Backlog

Description

@peteygao

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for a bug report that matches the one I want to file, without success.

Electron Version

43.8.4

What operating system(s) are you using?

macOS

Operating System Version

macOS 26.6.2

What arch are you using?

arm64 (including Apple Silicon)

Last Known Working Electron version

No response

Does the issue also appear in Chromium / Google Chrome?

No. The write is Electron's own (shell/browser/preload_code_cache.cc). The thread pool behaviour under it is Chromium's.

Expected Behavior

app.quit() ends the process promptly after the last JavaScript has run, however busy the machine is.

Actual Behavior

On macOS, when every core is busy, an app that quits within its first minutes on a new profile can hang after its
last JavaScript (process.on('exit')) until the load ends. The main thread sits in:

content::BrowserMainRunnerImpl::Shutdown
content::BrowserMainLoop::ShutdownThreadsAndCleanUp
base::internal::TaskTracker::CompleteShutdown
base::WaitableEvent::Wait

and a ThreadPoolBackgroundWorker sits in:

base::internal::TaskTracker::RunSkipOnShutdown
electron::preload_code_cache::(anonymous namespace)::WriteToDisk
base::CreateDirectoryAndGetError
mkdir

(stacks from sample, named with the release's breakpad symbols).

Testcase Gist URL

No response

Additional Information

Why. shell/browser/preload_code_cache.cc SetFromRenderer posts the write with
{base::MayBlock(), base::TaskPriority::BEST_EFFORT, base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}. BEST_EFFORT work runs on a
QOS_CLASS_BACKGROUND thread on macOS, which gets no CPU (and throttled I/O) while the cores are busy, so the task stays started for
seconds or minutes. SKIP_ON_SHUTDOWN makes the pool's shutdown wait for a task that has started. The worker would raise its own
priority at shutdown, but only between tasks.

How to see it. A sandboxed window with a preload, a fresh userData, 2 × the cores in busy loops, quit 10 to 20 s after launch.
Measured here: 3 of 64 quits never ended by themselves and 10 more stalled 1 to 4 s at a load average of 50 to 215. An app's own test
suite, which uses a new profile for every test, hits it far more often.

Asked for. Any of:

  • post the write at USER_VISIBLE (it is a few hundred kilobytes, once a profile), or
  • write it atomically (base::ImportantFileWriter::WriteFileAtomically) and make it CONTINUE_ON_SHUTDOWN, so a torn file is
    impossible and the quit never waits, or
  • give apps a way to turn the preload code cache off.

Related, same class.

  • DevToolsFileSystemIndexer is a default member of every electron::api::WebContents (electron_api_web_contents.h). Its constructor
    and its destructor each post a BEST_EFFORT task, and the destructor's is posted 13 to 19 ms after the app's last JavaScript, right
    before the pool shuts down. Creating the indexer only when DevTools first indexes a path would remove both.
  • ElectronBrowserClient does not override ContentBrowserClient::IsShuttingDown(), so GetNetworkService() starts the network
    service again when it ends during a quit ("Network service crashed or was terminated, restarting service."), and each start posts
    CalculateAndSetSharedDictionaryCacheMaxSize on a background thread.

Measured on an Apple M1 Pro (8 performance cores, 2 efficiency cores), Chromium 150.0.7871.250.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions