{"record":{"id":"cff64e0673f73b30","repo":"xai-org/x-algorithm","slug":"async-emb-context-not-initialized","errorCode":null,"errorMessage":"async_emb context not initialized","messagePattern":"async_emb context not initialized","errorType":"exception","errorClass":"std::invalid_argument","httpStatus":null,"severity":"error","filePath":"phoenix/xrex/cuda/async_emb/src/async_emb_api.cc","lineNumber":738,"sourceCode":"    return 0;\n  }\n  ctx->ensureHealthy();\n  uint64_t step = ctx->armedStep(pipeline);\n  if (step == 0) {\n    return 0;\n  }\n  if (!ctx->hostWaitDone(pipeline, step)) {\n    throw std::runtime_error(\n        \"async_emb: \" + std::string(label) + \" timed out waiting for step \" + std::to_string(step)\n    );\n  }\n  return step;\n}\n\nnb::bytes testSnapshot(int64_t context_id, const std::string& region) {\n  auto ctx = readyContext(context_id);\n  if (ctx == nullptr) {\n    throw std::invalid_argument(\"async_emb context not initialized\");\n  }\n  const auto& spec = ctx->spec();\n  const auto& layout = ctx->layout();\n  const size_t index_bytes =\n      size_t(ctx->worldSize()) * size_t(spec.tokens_per_rank) * sizeof(int32_t);\n  const size_t block_bytes = size_t(ctx->worldSize()) * size_t(spec.tokens_per_rank) *\n                             size_t(spec.shard_width) * sizeof(__nv_bfloat16);\n  using Operation = AsyncEmbContext::Operation;\n  struct Region {\n    Operation operation;\n    size_t offset;\n    size_t bytes;\n  };\n  const std::unordered_map<std::string, Region> regions = {\n      {\"lookup_ids\", {Operation::Lookup, layout.token_ids_all, index_bytes}},\n      {\"lookup_embeddings\", {Operation::Lookup, layout.lookup_recv, block_bytes}},\n      {\"lookup_send\", {Operation::Lookup, layout.lookup_send, block_bytes}},\n      {\"update_indices\", {Operation::Update, layout.segment_ids_all, index_bytes}},","sourceCodeStart":720,"sourceCodeEnd":756,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/xrex/cuda/async_emb/src/async_emb_api.cc#L720-L756","documentation":"async_emb resolves contexts by numeric id via readyContext. testSnapshot throws std::invalid_argument when the context id does not map to an initialized context (null result), i.e. the context was never created or has already been destroyed.","triggerScenarios":"Calling _test_snapshot(context_id, region) with a context_id that was never created via the module's init/creation API, or one whose context was torn down (training finished, context destroyed, or created in another process).","commonSituations":"Testing snapshot after training completed and the context was freed; passing 0 or a stale id; calling from a different process than the one that owns the context; ordering bug where the test runs before context creation.","solutions":["Create/initialize the async_emb context first and use the id it returns","Verify the training job / context that owns the id is still alive when snapshotting","Use the module's context-enumeration or lifecycle API (if available) to confirm the id before calling _test_snapshot"],"exampleFix":"# before\nsnap = async_emb._test_snapshot(ctx_id, \"grads\")  # ctx never created\n\n# after\nctx_id = async_emb.create_context(spec, ...)   # initialize first\nsnap = async_emb._test_snapshot(ctx_id, \"grads\")","handlingStrategy":"validation","validationCode":"ctx_id = create_context(spec)         # must succeed first\nassert ctx_id is not None and ctx_id > 0\n# only then snapshot\nsnap = async_emb._test_snapshot(ctx_id, \"grads\")","typeGuard":"def contextReady(ctx_id: int) -> bool:\n    return ctx_id > 0 and async_emb.has_context(ctx_id) if hasattr(async_emb, 'has_context') else ctx_id > 0","tryCatchPattern":"try:\n    snap = async_emb._test_snapshot(ctx_id, region)\nexcept ValueError as e:\n    if \"not initialized\" in str(e):\n        ctx_id = create_context(spec)\n        snap = async_emb._test_snapshot(ctx_id, region)\n    else:\n        raise","preventionTips":["Create contexts before use and keep ids in one owner object","Never snapshot after teardown"],"tags":["async-emb","cpp","python-binding","invalid-argument","lifecycle"],"backgroundTag":"invalid-context-handle","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}