Skip to content

Fix stale errno in listDir causing spurious ENOMEM crash#504

Open
caitengwei wants to merge 1 commit into
pytorch:mainfrom
caitengwei:fix/listdir-stale-errno
Open

Fix stale errno in listDir causing spurious ENOMEM crash#504
caitengwei wants to merge 1 commit into
pytorch:mainfrom
caitengwei:fix/listdir-stale-errno

Conversation

@caitengwei
Copy link
Copy Markdown

Summary

listDir() in gloo/common/linux.cc checks errno == 0 after the readdir loop to detect errors. However, vector::push_back inside the loop may internally trigger mallocmmap, which can transiently fail and set errno = ENOMEM even when the allocation ultimately succeeds via brk or retry. Since POSIX specifies that readdir does not modify errno on EOF, the stale value survives to the GLOO_ENFORCE check, causing a spurious crash.

Impact

We hit this in production after upgrading transformers from 4.57.1 to 5.5.4. The new version calls importlib.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 transient mmap failures far more likely during the listDir("/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 listDir remained.

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 via objdump disassembly (locating _M_realloc_insert in the readdir loop) and an LD_PRELOAD probe that caught errno=12 at readdir EOF after all 623 directory entries were successfully read.

Fix

Clear errno after each push_back call so that only readdir's own error state is visible after the loop. This is safe because push_back throws std::bad_alloc on real allocation failure — a successful return means the stale errno is a false positive.

Test

Added PciDevicesDoesNotCrashWithStaleErrno to linux_test.cc which sets errno = ENOMEM before calling pciDevices() (the public API that invokes listDir) and verifies no exception is thrown.

…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.
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 23, 2026

Hi @caitengwei!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 23, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Apr 23, 2026
@caitengwei
Copy link
Copy Markdown
Author

Could you please help review this pr? @d4l3k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant