zig
                                
                                 zig copied to clipboard
                                
                                    zig copied to clipboard
                            
                            
                            
                        race condition when running two compilations with empty cache
Zig Version
0.10.0-dev.3013+e867127a6
Steps to Reproduce
If two zig builds with no cached items are run on a macbook m1, it will hit an unreachable.
export ZIG_LOCAL_CACHE_DIR=/tmp/zig-cache
export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR
rm -rf /tmp/zig-cache
zig build-exe hello.zig &
zig build-exe hello.zig &
pub fn main() !void {}
Expected Behavior
Should compile.
Actual Behavior
panics with
thread 6473329 panic: reached unreachable code
/Users/andriusb/Projects/zig/zig/src/Module.zig:3249:31: 0x100f6ccc7 in Module.astGenFile (zig1)                                                
        error.FileNotFound => unreachable, // no dir components         
                              ^                                         
/Users/andriusb/Projects/zig/zig/src/Compilation.zig:3285:19: 0x100f6b9c3 in Compilation.workerAstGenFile (zig1)          
    mod.astGenFile(file) catch |err| switch (err) {                                                                                             
                  ^                                                     
/Users/andriusb/Projects/zig/zig/src/Module.zig:3249:31: 0x1003eccc7 in Module.astGenFile (zig1)                                                
        error.FileNotFound => unreachable, // no dir components                                                                                 
                              ^                                         
/Users/andriusb/Projects/zig/zig/src/Compilation.zig:3285:19: 0x1003eb9c3 in Compilation.workerAstGenFile (zig1)                                
    mod.astGenFile(file) catch |err| switch (err) {                                                                                             
                  ^               
/Users/andriusb/Projects/zig/zig/src/ThreadPool.zig:90:13: 0x10112495f in ThreadPool.Closure.runFn (zig1)                                       
            @call(.{}, func, closure.arguments);                                                                                                
            ^                   
/Users/andriusb/Projects/zig/zig/src/ThreadPool.zig:129:18: 0x100ce766f in ThreadPool.worker (zig1)                                             
            runFn(&run_node.data);                                                                                                              
                 ^                                                                                                                              
/Users/andriusb/Projects/zig/zig/lib/std/Thread.zig:393:13: 0x100f4f78b in std.Thread.callFn (zig1)                                             
            @call(.{}, f, args);                                        
            ^                                                           
/Users/andriusb/Projects/zig/zig/src/ThreadPool.zig:90:13: 0x1005a495f in ThreadPool.Closure.runFn (zig1)                                       
            @call(.{}, func, closure.arguments);                        
            ^                                                           
/Users/andriusb/Projects/zig/zig/src/ThreadPool.zig:129:18: 0x10016766f in ThreadPool.worker (zig1)
            runFn(&run_node.data);
                 ^
/Users/andriusb/Projects/zig/zig/lib/std/Thread.zig:667:30: 0x100f4f2fb in std.Thread.Instance.entryFn (zig1)
                return callFn(f, args_ptr.*);
originating from here https://github.com/ziglang/zig/blob/e867127a6cc3e0148101f08d846c876f62e839c1/lib/std/os.zig#L1680
according to man: A component of the path name that must exist does not exist.
I think both processes create the directory, but only one does it succesfully.
I have to add that I cannot reproduce this on linux, only on my macosx on m1
I'm really confused about this. How can it be that macOS is returning ENOENT when trying to openat with O_CREAT? Is this a kernel bug?
I'm seeing the same thing on the CI and still confused.
thread 199943 panic: attempt to unwrap error: FileNotFound
/Users/runner/work/zig/zig/lib/std/os.zig:1733:23: 0x10f29bd93 in openatZ (zig)
            .NOENT => return error.FileNotFound,
                      ^
/Users/runner/work/zig/zig/lib/std/fs.zig:1335:13: 0x10f0a0fe0 in createFileZ (zig)
            try os.openatZ(self.fd, sub_path_c, os_flags, flags.mode);
            ^
/Users/runner/work/zig/zig/lib/std/fs.zig:1274:9: 0x10ef1609c in createFile (zig)
        return self.createFileZ(&path_c, flags);
        ^
/Users/runner/work/zig/zig/src/Module.zig:3583:31: 0x10f32a85e in astGenFile (zig)
        error.FileNotFound => unreachable, // no dir components
                              ^
/Users/runner/work/zig/zig/src/Compilation.zig:3418:19: 0x10f32df92 in workerAstGenFile (zig)
    mod.astGenFile(file) catch |err| switch (err) {
                  ^
/Users/runner/work/zig/zig/lib/std/Thread/Pool.zig:94:39: 0x10f32de0e in runFn (zig)
            @call(.auto, func, closure.arguments);
                                      ^
/Users/runner/work/zig/zig/lib/std/Thread/Pool.zig:133:28: 0x10f08d3b0 in worker (zig)
            runFn(&run_node.data);
                           ^
/Users/runner/work/zig/zig/lib/std/Thread.zig:406:13: 0x10f2dffca in callFn__anon_95496 (zig)
            @call(.auto, f, args);
            ^
/Users/runner/work/zig/zig/lib/std/Thread.zig:679:30: 0x10f08dd8c in entryFn (zig)
                return callFn(f, args_ptr.*);
                             ^
This is an openat() call with O_CREAT with a pathname that has no directory components. I have no idea how it can be returning ENOENT.
Sorry for not answering.
To be honest I don't know if it is a kernel bug.
Thanks for merging!