Avoid calling GetTempFileName in BitmapDownload#11455
Avoid calling GetTempFileName in BitmapDownload#11455apoorvdarshan wants to merge 1 commit intodotnet:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #259 by replacing the Win32 GetTempFileName P/Invoke with Path.GetRandomFileName() + CREATE_NEW in a retry loop. The change prevents IOException when the temporary folder contains 65k+ files, which is a known limitation of the Win32 GetTempFileName API.
Changes:
- Removed the
GetTempFileNameP/Invoke wrapper fromUnsafeNativeMethodsOther.cs(no longer used) - Added
CREATE_NEWconstant toNativeMethodsOther.csfor proper file creation semantics - Replaced
GetTempFileNameusage inBitmapDownload.BeginDownloadwith a retry loop usingPath.GetRandomFileName()andCREATE_NEWdisposition
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsOther.cs | Removed unused GetTempFileName P/Invoke wrapper and related code |
| src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/NativeMethodsOther.cs | Added CREATE_NEW constant for Win32 CreateFile disposition |
| src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDownload.cs | Replaced GetTempFileName with retry loop using Path.GetRandomFileName() and CREATE_NEW; removed unused imports |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDownload.cs
Outdated
Show resolved
Hide resolved
|
A better solution would be to directly use |
0d5645b to
a9c8921
Compare
|
Thanks for the suggestion @h3xds1nz! You're right — Path.GetTempFileName on .NET 8+ already handles this internally with random filenames and retry logic. I've simplified the PR to just replace the Win32 P/Invoke with Path.GetTempFileName() directly. |
|
@apoorvdarshan It can still throw an exception, so for correctness, it should be under the exception handler; though it's very unlikely anyone's gonna be that lucky in their lifetime (but exceptional cases, hey). Other issue is that this implementation now changes the observable behaviour as the files will be now stored in |
Replace the Win32 GetTempFileName P/Invoke with Path.GetTempFileName, whose .NET 8+ implementation avoids the 65k file limit by generating random filenames internally. Fixes dotnet#259
a9c8921 to
46df3cb
Compare
|
@h3xds1nz Good catch, moved it inside the try block. And yeah the location changes but since it's DELETE_ON_CLOSE it shouldn't really matter in practice. |
|
Reference: #696 |
Summary
GetTempFileNameP/Invoke inBitmapDownload.BeginDownloadwithPath.GetRandomFileName()+CREATE_NEWin a retry loop, avoidingIOExceptionwhen the folder contains 65k+ temp filesCREATE_NEWnative constant and remove the now-unusedGetTempFileNameP/Invoke wrapperFileHelper.CreateAndOpenTemporaryFilein the codebaseFixes #259
Test plan
DELETE_ON_CLOSEsemantics)BitmapDecoderasync download pathGetTempFileNameP/Invoke has no other callers in production codeMicrosoft Reviewers: Open in CodeFlow