fix #14740: attribute teams helper processes, queue end-of-call prompts

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-05 15:48:55 +01:00
co-authored by Claude Opus 5.5
parent d631252600
commit bac30205d6
7 changed files with 527 additions and 93 deletions
+210 -1
View File
@@ -5,7 +5,13 @@
//! process *tree*, so the recorder needs the top-most process of the app:
//! walk up the parent chain while the parent runs the same executable.
//!
//! The walk is a pure function over a process table so it can be tested
//! Some apps capture the mic in a helper with a different exe: new Teams
//! (since Jan 2026) in `ms-teams_modulehost.exe`, earlier in
//! `msedgewebview2.exe` children. [`attribute`] maps those helpers to their
//! Teams ancestor, and only those: no other exe boundary is ever crossed, so
//! e.g. an app launched from a browser is never attributed to the browser.
//!
//! The walks are pure functions over a process table so they can be tested
//! without a live system; Windows builds the table from a ToolHelp snapshot.
// Only the Windows session enumeration calls into this module.
@@ -17,9 +23,64 @@ use std::collections::{HashMap, HashSet};
/// long chain) can never make the walk run away.
const MAX_STEPS: usize = 64;
/// Helper processes that may hold the mic on behalf of Teams.
const TEAMS_HELPERS: [&str; 2] = ["ms-teams_modulehost.exe", "msedgewebview2.exe"];
/// The Teams executables (new and classic) the helpers are attributed to.
const TEAMS_HOSTS: [&str; 2] = ["ms-teams.exe", "teams.exe"];
/// pid -> (parent pid, executable file name).
pub(crate) type ProcessTable = HashMap<u32, (u32, String)>;
fn is_one_of(exe: &str, names: &[&str]) -> bool {
names.iter().any(|name| exe.eq_ignore_ascii_case(name))
}
/// The app a mic session belongs to: `(exe, root_pid)`.
///
/// Normally that is the session process's own `exe` and its same-exe
/// [`root_pid`]. A Teams helper (`exe` in [`TEAMS_HELPERS`]) is attributed to
/// the nearest Teams ancestor instead, reached through helpers only, so
/// process loopback of that ancestor's tree covers the helper too. The walk
/// trusts a parent only if it was created strictly before its child
/// (`created` returns a process's creation time); otherwise the parent pid
/// was reused by an unrelated process, or the time is unknown, and the walk
/// stops. Without a Teams ancestor the session is reported as is.
pub(crate) fn attribute(
table: &ProcessTable,
pid: u32,
exe: &str,
created: impl Fn(u32) -> Option<u64>,
) -> (String, u32) {
let as_is = || (exe.to_string(), root_pid(table, pid));
if !is_one_of(exe, &TEAMS_HELPERS) {
return as_is();
}
let mut child = pid;
let mut seen = HashSet::from([pid]);
for _ in 0..MAX_STEPS {
let Some(&(parent, _)) = table.get(&child) else {
break;
};
let Some((_, parent_exe)) = table.get(&parent) else {
break;
};
let (Some(child_created), Some(parent_created)) = (created(child), created(parent)) else {
break;
};
if parent_created >= child_created || !seen.insert(parent) {
break;
}
if is_one_of(parent_exe, &TEAMS_HOSTS) {
return (parent_exe.clone(), root_pid(table, parent));
}
if !is_one_of(parent_exe, &TEAMS_HELPERS) {
break;
}
child = parent;
}
as_is()
}
/// The top-most ancestor of `pid` reachable through parents that run the
/// same executable (compared case-insensitively). Returns `pid` itself if it
/// is unknown or its parent runs something else. Cycles (possible after pid
@@ -48,6 +109,154 @@ pub(crate) fn root_pid(table: &ProcessTable, pid: u32) -> u32 {
mod tests {
use super::*;
/// A process table plus creation times, in "pid starts" order: each
/// process is created at tick == its position in `entries`, unless
/// overridden.
struct World {
table: ProcessTable,
created: HashMap<u32, u64>,
}
impl World {
fn new(entries: &[(u32, u32, &str)]) -> Self {
Self {
table: table(entries),
created: entries
.iter()
.enumerate()
.map(|(tick, &(pid, _, _))| (pid, tick as u64))
.collect(),
}
}
fn attribute(&self, pid: u32) -> (String, u32) {
let exe = self.table[&pid].1.clone();
attribute(&self.table, pid, &exe, |pid| {
self.created.get(&pid).copied()
})
}
}
#[test]
fn teams_modulehost_is_attributed_to_its_teams_root() {
let w = World::new(&[
(4, 0, "explorer.exe"),
(100, 4, "ms-teams.exe"),
(110, 100, "ms-teams.exe"),
(200, 110, "ms-teams_modulehost.exe"),
]);
assert_eq!(w.attribute(200), ("ms-teams.exe".to_string(), 100));
}
#[test]
fn webview_under_modulehost_under_teams_is_attributed_to_teams() {
let w = World::new(&[
(4, 0, "explorer.exe"),
(100, 4, "ms-teams.exe"),
(200, 100, "ms-teams_modulehost.exe"),
(300, 200, "msedgewebview2.exe"),
(310, 300, "msedgewebview2.exe"),
]);
assert_eq!(w.attribute(310), ("ms-teams.exe".to_string(), 100));
}
#[test]
fn classic_teams_webview_is_attributed_to_teams() {
let w = World::new(&[
(4, 0, "explorer.exe"),
(100, 4, "Teams.exe"),
(300, 100, "msedgewebview2.exe"),
]);
assert_eq!(w.attribute(300), ("Teams.exe".to_string(), 100));
}
#[test]
fn webview_under_outlook_is_unchanged() {
let w = World::new(&[
(4, 0, "explorer.exe"),
(100, 4, "outlook.exe"),
(300, 100, "msedgewebview2.exe"),
(310, 300, "msedgewebview2.exe"),
]);
assert_eq!(w.attribute(310), ("msedgewebview2.exe".to_string(), 300));
}
#[test]
fn a_parent_created_after_the_child_is_a_reused_pid() {
let mut w = World::new(&[
(4, 0, "explorer.exe"),
(100, 4, "ms-teams.exe"),
(200, 100, "ms-teams_modulehost.exe"),
]);
// The real parent exited and pid 100 went to a new Teams process.
w.created.insert(100, 50);
w.created.insert(200, 10);
assert_eq!(
w.attribute(200),
("ms-teams_modulehost.exe".to_string(), 200)
);
// Same creation time is not "earlier" either.
w.created.insert(100, 10);
assert_eq!(
w.attribute(200),
("ms-teams_modulehost.exe".to_string(), 200)
);
}
#[test]
fn an_unknown_creation_time_stops_the_walk() {
let mut w = World::new(&[
(4, 0, "explorer.exe"),
(100, 4, "ms-teams.exe"),
(200, 100, "ms-teams_modulehost.exe"),
]);
w.created.remove(&100);
assert_eq!(
w.attribute(200),
("ms-teams_modulehost.exe".to_string(), 200)
);
}
#[test]
fn other_exes_never_cross_an_exe_boundary() {
let w = World::new(&[
(4, 0, "explorer.exe"),
(100, 4, "chrome.exe"),
(200, 100, "zoom.exe"),
(300, 4, "ms-teams.exe"),
(400, 300, "microsip.exe"),
]);
assert_eq!(w.attribute(200), ("zoom.exe".to_string(), 200));
assert_eq!(w.attribute(400), ("microsip.exe".to_string(), 400));
}
#[test]
fn the_helper_walk_does_not_pass_through_other_exes() {
// A webview launched by some app that was itself started by Teams.
let w = World::new(&[
(4, 0, "explorer.exe"),
(100, 4, "ms-teams.exe"),
(200, 100, "someapp.exe"),
(300, 200, "msedgewebview2.exe"),
]);
assert_eq!(w.attribute(300), ("msedgewebview2.exe".to_string(), 300));
}
#[test]
fn the_helper_walk_survives_cycles() {
let w = World::new(&[
(200, 300, "ms-teams_modulehost.exe"),
(300, 200, "msedgewebview2.exe"),
]);
// Creation order makes 300 newer than 200, so the walk stops; with
// equal times it would still end on the cycle check.
assert_eq!(
w.attribute(200),
("ms-teams_modulehost.exe".to_string(), 200)
);
}
fn table(entries: &[(u32, u32, &str)]) -> ProcessTable {
entries
.iter()
+29 -6
View File
@@ -5,23 +5,25 @@
//! process (`IAudioSessionControl2::GetProcessId`) and is active while one
//! of its streams runs, i.e. while the app actually records the mic. The
//! process's exe comes from `QueryFullProcessImageNameW`; its `root_pid` is
//! the top of its same-exe parent chain in a ToolHelp snapshot (see
//! [`crate::process_tree`]), which is the tree application loopback records.
//! the top of its same-exe parent chain in a ToolHelp snapshot, which is the
//! tree application loopback records. Teams helper processes are reported
//! as their Teams ancestor (see [`crate::process_tree`]).
use std::path::Path;
use recorder_core::detect::MicSession;
use wasapi::{deinitialize, initialize_mta, DeviceEnumerator, Direction, SessionState};
use windows::core::PWSTR;
use windows::Win32::Foundation::{CloseHandle, HANDLE};
use windows::Win32::Foundation::{CloseHandle, FILETIME, HANDLE};
use windows::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
};
use windows::Win32::System::Threading::{
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION,
GetProcessTimes, OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32,
PROCESS_QUERY_LIMITED_INFORMATION,
};
use crate::process_tree::{root_pid, ProcessTable};
use crate::process_tree::{attribute, ProcessTable};
/// Sessions on every active capture endpoint, one entry per session.
///
@@ -52,10 +54,11 @@ fn enumerate() -> Vec<MicSession> {
raw.into_iter()
.filter_map(|(pid, active)| {
let exe = image_name(pid)?;
let (exe, root_pid) = attribute(&table, pid, &exe, creation_time);
Some(MicSession {
exe,
pid,
root_pid: root_pid(&table, pid),
root_pid,
active,
})
})
@@ -121,6 +124,26 @@ fn image_name(pid: u32) -> Option<String> {
}
}
/// When `pid` was created (FILETIME ticks), or `None` if the process cannot
/// be opened.
fn creation_time(pid: u32) -> Option<u64> {
// SAFETY: plain Win32 calls; the handle is closed below and the
// FILETIMEs outlive the call that fills them.
unsafe {
let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid).ok()?;
let (mut created, mut exited, mut kernel, mut user) = (
FILETIME::default(),
FILETIME::default(),
FILETIME::default(),
FILETIME::default(),
);
let result = GetProcessTimes(handle, &mut created, &mut exited, &mut kernel, &mut user);
close(handle);
result.ok()?;
Some((u64::from(created.dwHighDateTime) << 32) | u64::from(created.dwLowDateTime))
}
}
/// pid -> (parent pid, exe) of every process, from one ToolHelp snapshot.
/// Empty if the snapshot fails, which makes every session its own root.
fn process_table() -> ProcessTable {