@@ -159,6 +159,10 @@ impl IpcHost {
159159 PCWSTR ( name_wide. as_ptr ( ) ) ,
160160 ) ;
161161
162+ // Check for ERROR_ALREADY_EXISTS immediately after CreateFileMappingW
163+ // (before any other calls that might change GetLastError)
164+ let already_exists = GetLastError ( ) == ERROR_ALREADY_EXISTS ;
165+
162166 // Clean up security resources
163167 let _ = LocalFree ( Some ( std:: mem:: transmute :: < * mut ACL , HLOCAL > ( acl) ) ) ;
164168 let _ = LocalFree ( Some ( std:: mem:: transmute :: < PSID , HLOCAL > ( sid_uwp) ) ) ;
@@ -167,7 +171,7 @@ impl IpcHost {
167171 let handle = handle?;
168172
169173 // Check if the mapping already existed (indicates duplicate dump attempt)
170- if GetLastError ( ) == ERROR_ALREADY_EXISTS {
174+ if already_exists {
171175 CloseHandle ( handle) ?;
172176 return Err ( Error :: new (
173177 HRESULT ( ERROR_ALREADY_EXISTS . 0 as i32 ) ,
@@ -525,6 +529,26 @@ mod tests {
525529 assert ! ( result. is_err( ) ) ;
526530 }
527531
532+ #[ tokio:: test]
533+ async fn test_ipc_host_duplicate_fails ( ) {
534+ let pid = unique_pid ( ) ;
535+ let _host1 = IpcHost :: create ( pid) . expect ( "Failed to create first IPC host" ) ;
536+
537+ // Second create should fail with ERROR_ALREADY_EXISTS
538+ let result = IpcHost :: create ( pid) ;
539+ match result {
540+ Ok ( _) => panic ! ( "Second IpcHost::create should have failed" ) ,
541+ Err ( err) => {
542+ // Check that the error message mentions the duplicate session
543+ assert ! (
544+ err. message( ) . contains( "already exists" ) ,
545+ "Error should mention 'already exists': {}" ,
546+ err. message( )
547+ ) ;
548+ }
549+ }
550+ }
551+
528552 #[ tokio:: test]
529553 async fn test_ipc_start_dump_signal ( ) {
530554 let pid = unique_pid ( ) ;
0 commit comments