SoundPlayer.Play() does not preserve in-memory WAV buffer lifetime
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs, especially Play(), PlayLooping(), and the PlaySoundW import, to trace how _streamData is passed to asynchronous playback and how stopping or replacing playback is handled. Then find the related SoundPlayer tests and run them. Done means in-memory WAV data stays at a stable address until asynchronous playback completes or looping playback stops, while synchronous and file-path playback remain unaffected.
由索引模型根据 Issue 内容生成。
描述
Description
For Stream and non-file URL sources, SoundPlayer copies the WAV into its private _streamData array. Play() and PlayLooping() then call PlaySoundW with SND_MEMORY | SND_ASYNC (plus SND_LOOP for looping).
The generated [LibraryImport] stub pins the managed array only for the duration of the P/Invoke call. PlaySoundW returns immediately under SND_ASYNC, ending that pin while playback may still be using the buffer. The Win32 contract requires the buffer to remain valid until playback completes.
Required: pin [ PlaySoundW call ---------------- playback complete/stop ]
Actual: pin [ PlaySoundW call ] return ------- playback may continue
Why this is a product bug
This occurs through a supported public API path, such as new SoundPlayer(Stream).Play() or PlayLooping(). SoundPlayer allocates and owns the private _streamData copy, selects SND_MEMORY | SND_ASYNC, and initiates the native operation. The caller cannot access or pin _streamData, and Play() exposes no completion task or callback through which the caller could maintain its address.
The lifetime obligation therefore belongs to SoundPlayer: it must satisfy the downstream WinMM contract for the operation it chose. This is not caller misuse or a general thread-safety issue.
Impact
The generated P/Invoke pins _streamData only for the duration of the PlaySoundW call. Because SND_ASYNC returns when playback starts, WinMM may continue accessing the buffer after the pin is released.
The PlaySound contract requires a buffer used with SND_MEMORY | SND_ASYNC to remain valid until playback completes. A compacting GC can move _streamData, and replacing Stream can clear the final managed reference, allowing the array to be collected and its address reused while playback is active.
A diagnostic reproduction can observe the private array moving:
using var player = new SoundPlayer(new MemoryStream(wav, writable: false));
player.Load();
player.PlayLooping();
GC.Collect(
GC.MaxGeneration,
GCCollectionMode.Forced,
blocking: true,
compacting: true);
Private reflection is needed only to compare the array's address before and after the collection, not to create the underlying lifetime condition.
Expected behavior
Keep the in-memory WAV at a stable address until one-shot playback completes or looping playback is stopped, for example through a persistent pin or native-owned stable copy.
PlaySync() is not affected by this specific issue because PlaySoundW returns only after synchronous playback completes. File-path playback is also unaffected because Windows receives a filename rather than a managed WAV pointer.
- 主要语言
- C#
- 星标
- 18.4k
- 派生
- 5.8k
- 平均合并
- 2 天 12 小时
- 30 天内合并 PR
- 581
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/runtime 的其他 Issue
-
area-System.Collections untriaged
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#137167 · 1 条评论 ·
维护者通常 1 天内回复
-
Incorrect loop bound in SizeOptIListSelectIterator.ToList可能已有人在做 @equmagg 于 2 天前认领。 未关闭area-System.Collections untriaged
难度 2/5 1-3 小时 新手友好度 64/100
dotnet/runtime#137126 · 1 条评论 ·
维护者通常 1 天内回复
-
DbTransaction.SupportsSavepoints docs say Release/ReleaseAsync are expected to throw, but they are no-ops by design可能已有人在做 @alaricd 于 4 天前认领。 未关闭area-System.Transactions untriaged
难度 2/5 1-3 小时 新手友好度 72/100
dotnet/runtime#137040 · 1 条评论 ·
维护者通常 1 天内回复
-
FileSystemGlobbing: Matcher throws ArgumentOutOfRangeException when the rootDir is the same value as the file可能已有人在做 @makazeu 于 4 天前认领。 未关闭area-Extensions-FileSystem
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#136989 · 1 条评论 ·
维护者通常 1 天内回复
-
Configuration binder source generator emits unqualified List<T> when binding arrays without implicit usings可能已有人在做 @makazeu 于 4 天前认领。 未关闭area-Extensions-Configuration bug source-generator
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#136972 · 1 条评论 ·
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
sebastienros/jint#4531 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 65/100
NLog/NLog.MailKit#264 ·
维护者通常 1 天内回复
-
Unable to build未关闭Bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
Assert.That on a null non-generic IEnumerable throws ArgumentNullException, so IsNull() cannot pass未关闭bug P2
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
area/automation area/sdk kind/bug
难度 2/5 1-3 小时 新手友好度 78/100
unoplatform/uno.templates#2406 ·
维护者通常 1 天内回复