{"record":{"id":"9f59011991f4735d","repo":"jax-ml/jax","slug":"attempting-to-swap-into-allocation-with-key-key","errorCode":null,"errorMessage":"Attempting to swap into allocation with `key` {key} that is not a `Buffer`.","messagePattern":"Attempting to swap into allocation with `key` (.+?) that is not a `Buffer`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/pallas/mosaic/interpret/shared_memory.py","lineNumber":659,"sourceCode":"      increment_clock: Whether to increment the given thread's vector clock.\n      thread: The thread that's writing into the buffer.\n      logging_info: Information about the source of the swap.\n\n    Returns:\n      - The contents of the range of the buffer (prior to the swap), or None if\n        accessing buffer contents bounds.\n      - The shape and dtype of the full content array of the buffer.\n      - The incremented vector clock for the given thread.\n        None if race detection is not enabled or if `increment_clock` is False.\n    \"\"\"\n    clock = None\n    with self.lock:\n      if self.detect_races and increment_clock:\n        clock = self.incr_clock(thread, take_lock=False)\n\n      buff = self.mem[key]\n      if not isinstance(buff, Buffer):\n        raise ValueError(\n            f\"Attempting to swap into allocation with `key` {key} that is not a\"\n            \" `Buffer`.\"\n        )\n\n      shape_and_dtype = ShapeAndDtype(buff.logical_shape, buff.dtype)\n\n      assert buff.dtype == value.dtype  # TODO(jburnim): Catch this statically.\n      # TODO(jburnim): Better error message if this raises?\n\n      try:\n        result = buff[rnge].copy()\n      except IndexError:\n        # `buf` was accessed with `rnge` entirely out of bounds.\n        result = None\n\n      if result is not None:\n        in_bounds_shape = result.shape\n","sourceCodeStart":641,"sourceCodeEnd":677,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/pallas/mosaic/interpret/shared_memory.py#L641-L677","documentation":"swap_buffer_content (used by semaphore-style swap ops) looked up a memory key whose stored object is not a Buffer. The atomic-swap emulation needs Buffer shape/dtype metadata.","triggerScenarios":"Calling SharedMemManager.swap or _swap with a key registered to a semaphore or other non-Buffer allocation.","commonSituations":"Key collisions between semaphores and buffers; misuse of swap semantics on non-buffer allocations in custom interpret-mode primitives.","solutions":["Verify the key is a Buffer allocation before swapping","Fix key allocation so semaphores and buffers use disjoint key spaces","Swap against the correct MemKey returned by allocate_buffer"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_buffer_key(mgr, key):\n    with mgr.lock:\n        return isinstance(mgr.mem.get(key), Buffer)","tryCatchPattern":"try:\n    out = mgr.swap(key, value, ...)\nexcept ValueError as e:\n    if 'not a `Buffer`' in str(e):\n        raise KeyError(f'{key} is not a buffer allocation') from e\n    raise","preventionTips":["Swap only against buffer keys; use semaphore APIs for semaphores","Avoid ID spaces where semaphore keys and buffer keys can collide"],"tags":["jax","pallas","mosaic","shared-memory","key-collision","swap"],"backgroundTag":"invalid-key-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}