From 5056bda043984953685bb4284fc698ab42418045 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 4 Oct 2024 16:16:43 -0400 Subject: [PATCH] LibCore: Ensure shared memory file names on macOS are unique At least on my mac, clock_gettime only provides millisecond resolution. So if many WebContent processes are opened at once, it is not unlikely that they will all create their backing stores within the same ms. When that happens, all but the first will fail (and crash). To prevent this, generate the shared memory file name based on the PID and a static counter. --- Userland/Libraries/LibCore/System.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index bab009c9fcd..9940473d3dc 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -528,9 +528,9 @@ ErrorOr anon_create([[maybe_unused]] size_t size, [[maybe_unused]] int opti return Error::from_errno(saved_errno); } #elif defined(AK_OS_BSD_GENERIC) || defined(AK_OS_EMSCRIPTEN) || defined(AK_OS_HAIKU) - struct timespec time; - clock_gettime(CLOCK_REALTIME, &time); - auto name = ByteString::formatted("/shm-{}{}", (unsigned long)time.tv_sec, (unsigned long)time.tv_nsec); + static size_t shared_memory_id = 0; + + auto name = ByteString::formatted("/shm-{}-{}", getpid(), shared_memory_id++); fd = shm_open(name.characters(), O_RDWR | O_CREAT | options, 0600); if (shm_unlink(name.characters()) == -1) {