How could I write a per-pixel spinlock in raygen shader?

I am trying to write a per-pixel spinlock for ray tracing application.

I use a very simple test case that 2 threads share the same output pixel as shown below.
If I write the same codes in a compute shader, it works well.
However if I write it in a raygen shader, it seems to be deadlocked or somehow stall forever.

I have no idea why. My device is laptop 3070, API is vulkan, shader language is GLSL.
Thank you!

void main() {
    // The resolution of the image, which is the same as the launch size:
    const ivec2 resolution = ivec2(pushConstants.resolution);
    const ivec2 pixel = ivec2(gl_LaunchIDEXT.xy);
    // If the pixel is outside of the image, don't do anything:
    if((pixel.x >= resolution.x) || (pixel.y >= resolution.y)) {
        return;
    }

    ivec2 newpixel = pixel;
    newpixel.x /= 2;

    bool keepWaiting = true;
    while(keepWaiting) {
        if(imageAtomicCompSwap(atomicMutex, newpixel, 0, 1) == 0) {
            // insert(...);
            // memoryBarrier();
            imageAtomicExchange(atomicMutex, newpixel, 0);
            keepWaiting = false;
        }
    }
}