{"record":{"id":"102f55efdd04b6a6","repo":"jax-ml/jax","slug":"profile-has-already-been-started-only-one-profile","errorCode":null,"errorMessage":"Profile has already been started. Only one profile may be run at a time.","messagePattern":"Profile has already been started\\. Only one profile may be run at a time\\.","errorType":"error_code","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"jax/_src/profiler.py","lineNumber":185,"sourceCode":"  Only one trace may be collected at a time. A RuntimeError will be raised if\n  :func:`start_trace` is called while another trace is running.\n\n  Args:\n    log_dir: The directory to save the profiler trace to (usually the\n      TensorBoard log directory).\n    create_perfetto_link: A boolean which, if true, creates and prints link to\n      the Perfetto trace viewer UI (https://ui.perfetto.dev). The program will\n      block until the link is opened and Perfetto loads the trace.\n    create_perfetto_trace: A boolean which, if true, additionally dumps a\n      ``perfetto_trace.json.gz`` file that is compatible for upload with the\n      Perfetto trace viewer UI (https://ui.perfetto.dev). The file will also be\n      generated if ``create_perfetto_link`` is true. This could be useful if you\n      want to generate a Perfetto-compatible trace without blocking the process.\n    profiler_options: Profiler options to configure the profiler for collection.\n  \"\"\"\n  with _profile_state.lock:\n    if _profile_state.profile_session is not None:\n      raise RuntimeError(\"Profile has already been started. \"\n                         \"Only one profile may be run at a time.\")\n    clear_metadata()\n    # Make sure backends are initialized before creating a profiler\n    # session. Otherwise on Cloud TPU, libtpu may not be initialized before\n    # creating the tracer, which will cause the TPU tracer initialization to\n    # fail and no TPU operations will be included in the profile.\n    xla_bridge.get_backend()\n\n    options = profiler_options\n    if options is None:\n      options = ProfileOptions()\n    set_metadata(\"jax_version\", jax_version_module.__version__)\n    jaxlib_version_str = \".\".join(map(str, version_lib))\n    set_metadata(\"jaxlib_version\", jaxlib_version_str)\n    for backend_name in xla_bridge.backends():\n      try:\n        backend = xla_bridge.get_backend(backend_name)\n        set_metadata(f\"{backend.platform}_version\", backend.platform_version)","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/profiler.py#L167-L203","documentation":"jax.profiler.start_trace raises RuntimeError if a profiling session is already active — only one trace may run at a time per process, enforced via `_profile_state.profile_session`.","triggerScenarios":"Nested or overlapping `start_trace` calls: calling start_trace twice without stop_trace, or concurrent threads starting traces; the `trace` context manager used re-entrantly.","commonSituations":"Repeated notebook cells; concurrent profiling from multiple libraries; forgetting stop_trace after an exception bypassed the context manager.","solutions":["Always pair start_trace with stop_trace (prefer the `jax.profiler.trace()` context manager)","Call stop_trace before starting a new trace","Add locking/flags around programmatic profiling in multi-threaded code"],"exampleFix":"# before\njax.profiler.start_trace('/tmp/t1')\njax.profiler.start_trace('/tmp/t2')  # RuntimeError\n# after\nwith jax.profiler.trace('/tmp/t1'):\n  run_model()","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    jax.profiler.start_trace(log_dir)\nexcept RuntimeError as e:\n    if 'already been started' in str(e):\n        jax.profiler.stop_trace()\n        jax.profiler.start_trace(log_dir)\n    else:\n        raise","preventionTips":["Prefer `with jax.profiler.trace(...):` context manager","Never start traces from multiple threads without a lock"],"tags":["jax","profiler","concurrency","singleton"],"backgroundTag":"singleton-already-initialized","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}