Fix stale errno in listDir causing spurious ENOMEM crash#504
Conversation
…rash vector::push_back may internally call malloc→mmap which can transiently fail and set errno=ENOMEM even when the allocation ultimately succeeds via a different path. Since POSIX readdir does not clear errno on EOF, the stale ENOMEM survives to the GLOO_ENFORCE(errno == 0) check after the loop, causing a spurious crash. This is observed in practice on machines with many PCI devices (600+) under memory pressure from concurrent process spawning.
|
Hi @caitengwei! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Could you please help review this pr? @d4l3k |
Summary
listDir()ingloo/common/linux.cccheckserrno == 0after thereaddirloop to detect errors. However,vector::push_backinside the loop may internally triggermalloc→mmap, which can transiently fail and seterrno = ENOMEMeven when the allocation ultimately succeeds viabrkor retry. Since POSIX specifies thatreaddirdoes not modifyerrnoon EOF, the stale value survives to theGLOO_ENFORCEcheck, causing a spurious crash.Impact
We hit this in production after upgrading
transformersfrom 4.57.1 to 5.5.4. The new version callsimportlib.metadata.packages_distributions()at module level during import, increasing syscall volume by ~49% and heap pressure across all workers. When launching SGLang with TP=16 (mp.set_start_method("spawn")), 16 workers simultaneously import heavy Python packages and initialize Gloo process groups. The combined memory pressure makes transientmmapfailures far more likely during thelistDir("/sys/bus/pci/devices/")call (623 PCI entries on our machines), causing frequent crashes at startup.Rolling back to transformers 4.57.1 masked the issue (lower heap pressure → fewer transient mmap failures), but the underlying errno handling bug in
listDirremained.Reproduction
Observed on machines with 600+ PCI devices under memory pressure from 16 concurrent process spawns (e.g., PyTorch TP=16 with
mp.spawn). Confirmed viaobjdumpdisassembly (locating_M_realloc_insertin the readdir loop) and an LD_PRELOAD probe that caughterrno=12at readdir EOF after all 623 directory entries were successfully read.Fix
Clear
errnoafter eachpush_backcall so that onlyreaddir's own error state is visible after the loop. This is safe becausepush_backthrowsstd::bad_allocon real allocation failure — a successful return means the stale errno is a false positive.Test
Added
PciDevicesDoesNotCrashWithStaleErrnotolinux_test.ccwhich setserrno = ENOMEMbefore callingpciDevices()(the public API that invokeslistDir) and verifies no exception is thrown.