{"record":{"id":"6295cf20321887b5","repo":"apache/hadoop","slug":"threadjoin-waitforsingleobject-failed-with-error","errorCode":null,"errorMessage":"threadJoin: WaitForSingleObject failed with error %d\n","messagePattern":"threadJoin: WaitForSingleObject failed with error (.+?)\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/windows/thread.c","lineNumber":57,"sourceCode":"  HANDLE h;\n  h = CreateThread(NULL, 0, runThread, t, 0, NULL);\n  if (h) {\n    t->id = h;\n  } else {\n    ret = GetLastError();\n    fprintf(stderr, \"threadCreate: CreateThread failed with error %d\\n\", ret);\n  }\n  return ret;\n}\n\nint threadJoin(const thread *t) {\n  DWORD ret = WaitForSingleObject(t->id, INFINITE);\n  switch (ret) {\n  case WAIT_OBJECT_0:\n    break;\n  case WAIT_FAILED:\n    ret = GetLastError();\n    fprintf(stderr, \"threadJoin: WaitForSingleObject failed with error %d\\n\",\n      ret);\n    break;\n  default:\n    fprintf(stderr, \"threadJoin: WaitForSingleObject unexpected error %d\\n\",\n      ret);\n    break;\n  }\n  return ret;\n}\n","sourceCodeStart":39,"sourceCodeEnd":67,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/windows/thread.c#L39-L67","documentation":"Windows threadJoin: WaitForSingleObject on the thread HANDLE returned WAIT_FAILED and GetLastError gives the reason — most commonly ERROR_INVALID_HANDLE (6): the handle was closed elsewhere, double-joined, or never valid. Note this wrapper does not close the handle on success either, so ownership bugs surface exactly here.","triggerScenarios":"Joining a thread whose HANDLE was already closed by other cleanup code; double join on the same thread struct; passing an uninitialized thread struct.","commonSituations":"Refactors that add CloseHandle in a cleanup path; racing shutdown sequences closing handles before joins.","solutions":["For code 6, audit every CloseHandle call and every join path for double-close/double-join","Assign exactly one owner per thread handle: it joins, then closes","Zero-initialize thread structs and mark them invalid after join"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"/* Track handle validity so joins never see a stale handle */\ntypedef struct { HANDLE h; int valid; } thr_slot;\n/* set valid=0 immediately after WaitForSingleObject succeeds or the handle is closed */","typeGuard":null,"tryCatchPattern":"if (threadJoin(&t) != 0) {\n    /* WAIT_FAILED code printed; do not close or join again blindly */\n    log_error(\"join failed\");\n}","preventionTips":["One owner per thread handle: join then close, exactly once","Never CloseHandle before the matching join","Zero thread structs and mark them invalid after use"],"tags":["libhdfs","windows","waitforsingleobject","thread-join","win32"],"backgroundTag":"waitforsingleobject-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}