Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

WASAPI: default-device activation fails with RPC_E_CHANGED_MODE after endpoint-notification callbacks put thread-pool threads into an STA

オープン
#1,402 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@dannyward630 がすでに取り組んでいます。

2026年10月24日 から。

  • #1413 @dannyward630 による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
20/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
rust

調査の方向性

Start in src/host/wasapi/stream.rs at DefaultDeviceNotificationImpl::OnDeviceRemoved and OnDeviceStateChanged, then follow get_current_default into current_default_endpoint in device.rs to where com_initialized() is called. Run the reproduction in the issue, which polls the default output device after thread-pool callbacks, and confirm RPC_E_CHANGED_MODE on the second open. Done means the default device opens after those callbacks run, without changing the calling thread's apartment.

索引モデルが issue の本文から書いたものです。

説明

correctness host - wasapi platform - windows

cpal version: 0.18.2 (WASAPI host), Windows 11

What happens

After some time, opening the default input or output device fails for the rest of the process:

Failed to get audio client: Cannot change thread mode after it is set. (os error -2147417850)

(0x80010106 = RPC_E_CHANGED_MODE). Devices obtained from input_devices() / output_devices() (specific IMMDevices) keep working; only default_input_device() / default_output_device() fail. Only a restart of the process helps.

Cause
  1. DefaultDeviceNotificationImpl::OnDeviceRemoved and OnDeviceStateChanged (src/host/wasapi/stream.rs) call get_current_default → current_default_endpoint (device.rs), which calls com::com_initialized().
  2. These callbacks run on threads owned by Windows (MMDevAPI / thread pool), not on cpal's threads. com_initialized() calls CoInitializeEx(COINIT_APARTMENTTHREADED) there. On a pool thread that has no COM initialization of its own this succeeds, so the pool thread becomes an STA. The thread-local guard is only dropped when the thread exits, which pool threads normally do not, so the thread stays STA.
  3. For the default device, cpal activates via ActivateAudioInterfaceAsync(DEVINTERFACE_AUDIO_*) (activate_audio_interface_sync). When the process has a multithreaded apartment (any thread did CoInitializeEx(COINIT_MULTITHREADED), which many libraries do), the activation runs on the thread pool and fails with RPC_E_CHANGED_MODE on a pool thread that cpal turned into an STA.
Reproduction

Simulates step 1–2 by calling cpal's own path on pool threads (what the notification callbacks do):

use cpal::traits::{DeviceTrait, HostTrait};
use std::sync::atomic::{AtomicUsize, Ordering};
use windows::Win32::System::{Com::*, Threading::*};

static DONE: AtomicUsize = AtomicUsize::new(0);

unsafe extern "system" fn cb(_: PTP_CALLBACK_INSTANCE, _: *mut core::ffi::c_void) {
    let _ = cpal::default_host().default_output_device(); // -> current_default_endpoint -> com_initialized
    std::thread::sleep(std::time::Duration::from_millis(200));
    DONE.fetch_add(1, Ordering::SeqCst);
}

fn open() {
    std::thread::spawn(|| {
        let d = cpal::default_host().default_output_device().unwrap();
        println!("{:?}", d.supported_output_configs().map(|c| c.count()));
    })
    .join()
    .unwrap();
}

fn main() {
    // Some other thread in the MTA, as libraries commonly have.
    std::thread::spawn(|| {
        let _ = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
        loop { std::thread::park(); }
    });
    std::thread::sleep(std::time::Duration::from_millis(100));
    open(); // Ok(..)
    for _ in 0..64 {
        unsafe { TrySubmitThreadpoolCallback(Some(cb), None, None).unwrap() };
    }
    while DONE.load(Ordering::SeqCst) < 64 {
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    open(); // Err(Failed to get audio client: Cannot change thread mode after it is set. ...)
}

Observed: the first open() succeeds, every later one fails with RPC_E_CHANGED_MODE. Without the MTA thread both succeed. Calling a specific device's supported_output_configs() after the poisoning still succeeds.

Suggested fix

Code that runs on threads cpal does not own (the IMMNotificationClient callbacks, Drop of DefaultDeviceMonitor if it can run elsewhere) should not change that thread's apartment. For example, current_default_endpoint could skip com_initialized() when called from the callbacks, or join the MTA there (CoInitializeEx(COINIT_MULTITHREADED) / CoIncrementMTAUsage) instead of making the thread an STA. (Not verified: on which threads MMDevAPI invokes the callbacks; the reproduction above uses the default thread pool.)

Related: #1302 (enumerator bound to the first thread's apartment).

主要言語
Rust
スター
4k
フォーク
554
平均マージ
2日 11時間
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

RustAudio/cpal のほかの issue

RustAudio/cpal の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。