apache/hadoop · error

threadJoin: WaitForSingleObject failed with error

Error message

threadJoin: WaitForSingleObject failed with error %d

What it means

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.

Solutions

  1. For code 6, audit every CloseHandle call and every join path for double-close/double-join
  2. Assign exactly one owner per thread handle: it joins, then closes
  3. Zero-initialize thread structs and mark them invalid after join
Defensive patterns

Strategy: validation

Validate before calling

/* Track handle validity so joins never see a stale handle */
typedef struct { HANDLE h; int valid; } thr_slot;
/* set valid=0 immediately after WaitForSingleObject succeeds or the handle is closed */

Try / catch

if (threadJoin(&t) != 0) {
    /* WAIT_FAILED code printed; do not close or join again blindly */
    log_error("join failed");
}

Prevention

When it happens

Trigger: Joining a thread whose HANDLE was already closed by other cleanup code; double join on the same thread struct; passing an uninitialized thread struct.

Common situations: Refactors that add CloseHandle in a cleanup path; racing shutdown sequences closing handles before joins.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/6295cf20321887b5. Report an issue: GitHub.

Appendix: source

Thrown at hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/windows/thread.c:57

  HANDLE h;
  h = CreateThread(NULL, 0, runThread, t, 0, NULL);
  if (h) {
    t->id = h;
  } else {
    ret = GetLastError();
    fprintf(stderr, "threadCreate: CreateThread failed with error %d\n", ret);
  }
  return ret;
}

int threadJoin(const thread *t) {
  DWORD ret = WaitForSingleObject(t->id, INFINITE);
  switch (ret) {
  case WAIT_OBJECT_0:
    break;
  case WAIT_FAILED:
    ret = GetLastError();
    fprintf(stderr, "threadJoin: WaitForSingleObject failed with error %d\n",
      ret);
    break;
  default:
    fprintf(stderr, "threadJoin: WaitForSingleObject unexpected error %d\n",
      ret);
    break;
  }
  return ret;
}

View on GitHub (pinned to 2add963021)