|
| 1 | +"""Tests for ``raise_fd_limit`` — EMFILE / "too many open files" mitigation. |
| 2 | +
|
| 3 | +LanceDB's merge-insert path opens many file handles concurrently; under the |
| 4 | +default OS soft ``RLIMIT_NOFILE`` (256 on macOS GUI/launchd-launched processes) |
| 5 | +this exhausts file descriptors -> ``Too many open files (os error 24)`` in |
| 6 | +``lance-io/local.rs``. ``raise_fd_limit`` raises the process's own soft limit |
| 7 | +toward its hard limit so cocoindex children (which inherit rlimits) get headroom. |
| 8 | +
|
| 9 | +See https://github.com/HumanBean17/java-codebase-rag/issues/306 |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from java_codebase_rag import _fdlimit |
| 15 | + |
| 16 | + |
| 17 | +def test_raises_soft_limit_up_to_cap(monkeypatch): |
| 18 | + """When soft < min(hard, cap), raise soft to the target and keep hard.""" |
| 19 | + monkeypatch.setattr(_fdlimit.resource, "getrlimit", lambda _rlim: (256, 65536)) |
| 20 | + calls: list[tuple] = [] |
| 21 | + monkeypatch.setattr( |
| 22 | + _fdlimit.resource, "setrlimit", lambda rlim, limits: calls.append((rlim, limits)) |
| 23 | + ) |
| 24 | + |
| 25 | + _fdlimit.raise_fd_limit(cap=4096) |
| 26 | + |
| 27 | + assert calls == [(_fdlimit.resource.RLIMIT_NOFILE, (4096, 65536))] |
| 28 | + |
| 29 | + |
| 30 | +def test_caps_target_at_hard_limit(monkeypatch): |
| 31 | + """Never exceed the hard limit even when cap > hard.""" |
| 32 | + monkeypatch.setattr(_fdlimit.resource, "getrlimit", lambda _rlim: (256, 1024)) |
| 33 | + calls: list[tuple] = [] |
| 34 | + monkeypatch.setattr( |
| 35 | + _fdlimit.resource, "setrlimit", lambda rlim, limits: calls.append((rlim, limits)) |
| 36 | + ) |
| 37 | + |
| 38 | + _fdlimit.raise_fd_limit(cap=65536) # target = min(1024, 65536) = 1024 |
| 39 | + |
| 40 | + assert calls == [(_fdlimit.resource.RLIMIT_NOFILE, (1024, 1024))] |
| 41 | + |
| 42 | + |
| 43 | +def test_noop_when_soft_already_at_or_above_target(monkeypatch): |
| 44 | + """No setrlimit call when the soft limit is already high enough.""" |
| 45 | + monkeypatch.setattr(_fdlimit.resource, "getrlimit", lambda _rlim: (1048576, 1048576)) |
| 46 | + calls: list[tuple] = [] |
| 47 | + monkeypatch.setattr( |
| 48 | + _fdlimit.resource, "setrlimit", lambda rlim, limits: calls.append((rlim, limits)) |
| 49 | + ) |
| 50 | + |
| 51 | + _fdlimit.raise_fd_limit(cap=65536) |
| 52 | + |
| 53 | + assert calls == [] |
| 54 | + |
| 55 | + |
| 56 | +def test_noop_when_rlimit_nofile_unsupported(monkeypatch): |
| 57 | + """Windows-like host with no RLIMIT_NOFILE: no error, no setrlimit.""" |
| 58 | + monkeypatch.delattr(_fdlimit.resource, "RLIMIT_NOFILE") |
| 59 | + calls: list[tuple] = [] |
| 60 | + monkeypatch.setattr( |
| 61 | + _fdlimit.resource, "setrlimit", lambda *a, **k: calls.append((a, k)) |
| 62 | + ) |
| 63 | + |
| 64 | + _fdlimit.raise_fd_limit() # must not raise |
| 65 | + |
| 66 | + assert calls == [] |
| 67 | + |
| 68 | + |
| 69 | +def test_swallows_setrlimit_errors(monkeypatch): |
| 70 | + """Best-effort: a failing setrlimit must never propagate.""" |
| 71 | + monkeypatch.setattr(_fdlimit.resource, "getrlimit", lambda _rlim: (256, 65536)) |
| 72 | + |
| 73 | + def boom(rlim, limits): |
| 74 | + raise OSError("permission denied") |
| 75 | + |
| 76 | + monkeypatch.setattr(_fdlimit.resource, "setrlimit", boom) |
| 77 | + |
| 78 | + _fdlimit.raise_fd_limit(cap=4096) # must not raise |
0 commit comments