Use `NtQueryVolumeInformationFile` instead of `GetConsoleMode` to check whether a handle is a console #6483
All Vouch review confidence checks passed
Residual risk 0.29 = Change Risk (0.52) × (1 − Review Confidence (0.45)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 438 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.364
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #6483"
general
weight: 0.35
YexuanXiao
Earlier this year I looked into the Windows console in depth, and since it's open source, I had the opportunity to analyze how it's implemented.
Put simply, when program call
GetConsoleMode(as well as any other console API, and when reading from or writing to the console handle), the following happens:1. The app communicates with condrv.sys
2. condrv.sys communicates with conhost.exe (or openconsole.exe, which is the distributed version of conhost.exe) through a pipe-like file, where condrv.sys produces the following messages:
https://github.com/microsoft/terminal/blob/6676c6f938f7e98ad680f0cecedd3bafb6529734/dep/Console/condrv.h#L33-L40
3. conhost.exe processes these messages and write back
NtQueryVolumeInformationFileis not a console API; it merely queries the device (condrv.sys) info provided when the fs driver is registered (about 500 cycles), so it is much faster thanGetConsoleMode.This usage of NtQueryVolumeInformationFile has been supported since Windows 8, see <https://github.com/rust-lang/rust/pull/164170>.
[[print.fun] 10.2](https://eel.is/c++draft/iostream.format#print.fun-note-1) says that on Windows,
GetConsoleModeis used to determine whether a handle refers to a console, so an LWG issue may be needed. However, since there is no observable difference, this patch does not need to wait for standards.I just ran a simple test, and
NtQueryVolumeInformationFileis 100 times faster thanGetConsoleMode.test program:
I wrote a benchmark for
std::print, but the results fluctuate a lot.