Files
opencv/modules/core
Ghazi-raad 2cf9c68da0 Merge pull request #28087 from Ghazi-raad:fix/tempfile-race-condition-19648
Fix tempfile race condition on Windows (issue #19648) #28087

Fix tempfile race condition on Windows

Addresses issue #19648

Problem
The cv::tempfile() function on Windows used GetTempFileNameA() followed by an immediate DeleteFileA() call. This created a race condition where multiple OpenCV processes running simultaneously could receive the same temporary filename, leading to name collisions.

Root Cause
The previous implementation:

Called GetTempFileNameA() to generate a temp filename
Immediately deleted the file to free the name
Returned just the filename string
Between steps 2 and 3, another process could call GetTempFileNameA() and receive the same filename, causing a collision.

Solution
Replaced GetTempFileNameA() with GUID-based filename generation using CoCreateGuid(), following the same approach already used in GetTempFileNameWinRT() and Microsoft's recommendations for scenarios requiring many temp files.

Changes
modules/core/src/system.cpp:

Removed GetTempFileNameA() and DeleteFileA() calls
Added CoCreateGuid() to generate unique GUID-based filenames
Format: "ocv{GUID}" where GUID ensures uniqueness across processes
Benefits

Eliminates race condition in multi-process scenarios
No file I/O overhead from creating and deleting placeholder files
Consistent with WinRT implementation approach
Follows Microsoft best practices
Testing
Standard OpenCV test suite. The change only affects Windows temp file naming and maintains the same String return type and usage pattern.
2025-11-27 17:57:42 +03:00
..