{"record":{"id":"4f26b7ed3246f189","repo":"jax-ml/jax","slug":"semaphore-sem-id-occurs-as-both-fixed-id-and-int","errorCode":null,"errorMessage":"Semaphore {sem_id} occurs as both fixed-id and internal.","messagePattern":"Semaphore (.+?) occurs as both fixed-id and internal\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/pallas/mosaic/interpret/shared_memory.py","lineNumber":831,"sourceCode":"    device_id = global_core_id // self.num_cores_per_device\n    local_core_id = global_core_id % self.num_cores_per_device\n    return (device_id, local_core_id)\n\n  def print(self, device_id: int):\n    device_id = int(device_id)\n    if device_id == 0:\n      with self.lock:\n        print(self.mem)\n\n  def _unsafe_get_semaphore(self, sem_id: int) -> Semaphore:\n    \"\"\"Returns the semaphore with the given ID. `self.lock` must be held.\"\"\"\n\n    if sem_id in self.fixed_id_sem:\n      if sem_id in self.sem:\n        # TODO(nrink): For now we make it the responsibility of the client to\n        # ensure that fixed-ID semaphores do not collide with internal\n        # semaphore IDs.\n        raise ValueError(\n            f'Semaphore {sem_id} occurs as both fixed-id and internal.'\n        )\n      return self.fixed_id_sem[sem_id]\n    else:\n      return self.sem[sem_id]\n\n  def set_failed(\n      self,\n      exception: Exception,\n      device_id: int | None = None,\n      local_core_id: int | None = None,\n      top_level: bool = True,\n  ):\n    with self.lock:\n      if self._failure is None:\n        self._failure = exception\n        if device_id is not None and local_core_id is not None:\n          self._failed_thread = (device_id, local_core_id)","sourceCodeStart":813,"sourceCodeEnd":849,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/pallas/mosaic/interpret/shared_memory.py#L813-L849","documentation":"A semaphore ID was found in both the fixed-ID semaphore table and the internal semaphore table. Mosaic lets clients create semaphores with explicit IDs, but those must not collide with internally generated semaphore IDs.","triggerScenarios":"Creating a fixed-ID semaphore whose ID equals one already assigned to an internal semaphore in the same SharedMemManager, then looking it up via _unsafe_get_semaphore (used by semaphore ops and clock incrementing).","commonSituations":"Choosing small manual semaphore IDs (0, 1, 2...) that clash with internal allocation order; running multiple kernels sharing one manager; refactors changing internal semaphore allocation order.","solutions":["Use distinct, high or namespaced fixed semaphore IDs unlikely to collide","Let the library allocate semaphores internally instead of using fixed IDs when possible","Reproduce and print both self.fixed_id_sem and self.sem key sets to find the colliding ID"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"assert not (sem_id in mgr.fixed_id_sem and sem_id in mgr.sem), f'semaphore id {sem_id} collides'","typeGuard":null,"tryCatchPattern":"try:\n    sem = mgr._unsafe_get_semaphore(sem_id)\nexcept ValueError as e:\n    if 'both fixed-id and internal' in str(e):\n        sem = mgr.fixed_id_sem[sem_id]  # or re-register with a fresh id\n    else:\n        raise","preventionTips":["Allocate fixed semaphore IDs from a high, dedicated range","Prefer library-allocated semaphore IDs when fixed IDs aren't required"],"tags":["jax","pallas","mosaic","semaphore","id-collision","interpret-mode"],"backgroundTag":"id-collision","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}