Metal: protect tensor alloc/free byte counters with a mutex#420
Open
aledesogusbusiness-hue wants to merge 1 commit into
Open
Metal: protect tensor alloc/free byte counters with a mutex#420aledesogusbusiness-hue wants to merge 1 commit into
aledesogusbusiness-hue wants to merge 1 commit into
Conversation
g_tensor_alloc_live_bytes and g_tensor_alloc_peak_bytes were plain uint64_t globals updated by ds4_gpu_tensor_alloc() and ds4_gpu_tensor_free() without any synchronisation. When multiple worker threads concurrently allocate or free Metal tensors (e.g. during concurrent session cleanup and new task startup) the unguarded read-modify-write can corrupt the counters and, on ARM64, the non-atomic 64-bit access can race. Add g_tensor_alloc_mu (PTHREAD_MUTEX_INITIALIZER) and take it around every update and the diagnostic snapshot read. The fprintf stays outside the lock; it uses a local snapshot so the lock is never held across an I/O call. Fixes: antirez#404 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
g_tensor_alloc_live_bytesandg_tensor_alloc_peak_bytesinds4_metal.mare plainuint64_tglobals updated byds4_gpu_tensor_alloc()andds4_gpu_tensor_free()without any synchronisation.When multiple worker threads concurrently allocate or free Metal tensors (concurrent session cleanup racing with new task startup, or back-to-back requests on the same server), the unguarded read-modify-write is a data race. On ARM64 the 64-bit load/store is not guaranteed atomic, and the torn write can corrupt the counter or — when
reallocis called shortly after — trigger the malloc freelist corruption that shows up asEXC_BREAKPOINT / BUG IN CLIENT OF LIBMALLOCinds4_gpu_tensor_free.Fix
Add
g_tensor_alloc_mu(PTHREAD_MUTEX_INITIALIZER) and hold it around every update to the two counters and around the diagnostic snapshot read. Thefprintfis kept outside the lock (it uses a local snapshot) so no I/O is ever done under the mutex.Test plan
Fixes #404
🤖 Generated with Claude Code