{"record":{"id":"f81cbc628483388e","repo":"apache/hadoop","slug":"threadlocalstorageset-pthread-setspecific-failed","errorCode":null,"errorMessage":"threadLocalStorageSet: pthread_setspecific failed with error %d\n","messagePattern":"threadLocalStorageSet: pthread_setspecific 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/posix/thread_local_storage.c","lineNumber":187,"sourceCode":"  if (!gTlsKeyInitialized) {\n    ret = pthread_key_create(&gTlsKey, hdfsThreadDestructor);\n    if (ret) {\n      fprintf(stderr,\n        \"threadLocalStorageGet: pthread_key_create failed with error %d\\n\",\n        ret);\n      return ret;\n    }\n    gTlsKeyInitialized = 1;\n  }\n  *state = pthread_getspecific(gTlsKey);\n  return ret;\n}\n\nint threadLocalStorageSet(struct ThreadLocalState *state)\n{\n  int ret = pthread_setspecific(gTlsKey, state);\n  if (ret) {\n    fprintf(stderr,\n      \"threadLocalStorageSet: pthread_setspecific failed with error %d\\n\",\n      ret);\n    hdfsThreadDestructor(state);\n  }\n  return ret;\n}\n","sourceCodeStart":169,"sourceCodeEnd":194,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/thread_local_storage.c#L169-L194","documentation":"pthread_setspecific failed while binding the freshly built ThreadLocalState to the thread: EINVAL means gTlsKey is invalid (never created or destroyed), ENOMEM means insufficient memory. The wrapper then calls hdfsThreadDestructor(state), which frees the state, so it must not be used afterwards; getJNIEnv fails and the umbrella 'getGlobalJNIEnv failed' message follows.","triggerScenarios":"Torn initialization (key flagged initialized but actually invalid) or ENOMEM under memory pressure at first per-thread setup. Rare in practice.","commonSituations":"Same conditions as OOM; memory corruption of libhdfs globals; unsupported dlopen/dlclose reuse patterns.","solutions":["Address ENOMEM as an OOM condition (raise limits, fix leaks)","If EINVAL, suspect corruption of libhdfs globals or repeated dlclose/dlopen of the library; rebuild/upgrade libhdfs","Capture the numeric code from the message to distinguish ENOMEM from EINVAL"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if (anyHdfsCall() == -1 && errno == EINTERNAL) {\n    /* if stderr shows setspecific ENOMEM, address memory first */\n    free_cache();\n    retry_once();\n}","preventionTips":["Keep native heap headroom in memory-capped deployments","Do not dlopen/dlclose libhdfs repeatedly — its TLS state is process-wide","Distinguish ENOMEM from EINVAL in the message before choosing a fix"],"tags":["libhdfs","pthread","tls","thread-local-storage","out-of-memory"],"backgroundTag":"pthread-setspecific-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}