DEV Community

errno 27 (EFBIG): RLIMIT_FSIZE, SIGXFSZ, and the bytes that fit

TL;DR

EFBIG (27) is not a full disk. It is a per-process ceiling on how large one regular file may grow.

A 4,096-byte ceiling on a host with gigabytes free is a perfectly normal configuration.

A 4 MB log line that never appeared, a worker that vanished with exit status 153 and nothing in stderr, a copy that stopped exactly at a round number of bytes. Three different incidents, one cause: RLIMIT_FSIZE. This is a condensed field report on errno 27 (EFBIG) - what the kernel actually does, why C dies while your Python job sometimes raises, and the three checks that tell you which one you are looking at.

  • EFBIG (27) is not a full disk. It is a per-process ceiling on how large one regular file may grow. A 4,096-byte ceiling on a host with gigabytes free is a perfectly normal configuration.
  • A write that starts below the ceiling is clipped, not refused: it stores what fits and returns that smaller count, with no error at all.
  • A write that starts at or past the ceiling makes the kernel send SIGXFSZ (25) and return EFBIG at the same moment.
  • Which of those two reaches your code depends on the signal disposition. With the kernel default, the process is gone before the call returns: shell status 153 (128 + 25), no traceback, no log line.
  • A missing traceback is therefore not evidence of OOM. Check the exit status first.

The kernel path in four lines

Every buffered write on Linux passes through the same check, generic_write_check_limits() in fs/read_write.c. Stripped to its decision:

if (pos >= limit) /* the write starts at or past the ceiling */
  return -EFBIG; /* caller also gets SIGXFSZ */
if (count > limit - pos) /* the write would cross the ceiling */
  count = limit - pos; /* clip it, return the smaller count */

That asymmetry explains almost every symptom on this page. The first branch is a refusal; the second is a short write that reports success.

Two limits feed it: RLIMIT_FSIZE from the process, and s_maxbytes from the filesystem. On a 32-bit process opened without O_LARGEFILE there is a third, MAX_NON_LFS - same errno, older cause.

What I measured

Host: Linux kernel 6.12.118, x86_64, 2 vCPU, Python 3.12.16, uid 1000, /tmp on tmpfs and the workspace on virtiofs. Process file-size limit 536,870,912 bytes soft and hard; core limit 0, so a signal death leaves no core to inspect either.

Child processes were given a 4,096-byte limit and five runs per arm:

What the child did Result
write of exactly 4,096 bytes stored 4,096, returned 4,096 - every run
one byte more, after the file was full SIGXFSZ + EFBIG - never survived
one 5,000-byte write at offset 0 stored 4,096, returned 4,096 (no exception)
dd 8,192 bytes into the 4,096-byte limit killed by SIGXFSZ, status 153, empty stderr, 4,096-byte file on disk
the same write from Python, default disposition killed by SIGXFSZ, status 153
the same write from Python, SIGXFSZ ignored OSError(27, 'File too large'), file ends at 4,096

Two things fall out of that table. First, write() returning the number you passed is not proof that the bytes landed - for a clipped write it returns less, and plenty of callers only check for -1. Second, Python raising OSError where dd dies silently is not Python being safer by design; it is the disposition.

Why C dies and Python usually raises

The disposition is a property of the process, inherited across fork and exec. SIG_DFL for SIGXFSZ means terminate, so the write never returns and no frame ever resumed - nothing to log. SIG_IGN means the write returns -1 with EFBIG, which the runtime turns into an exception.

Four children, same 1,024-byte limit, same two writes, five runs each:

Disposition in the child Second write Process Status
as inherited from this host EFBIG survived 0
SIG_DFL set explicitly SIGXFSZ killed, empty stderr 153
SIG_IGN EFBIG survived 0
a Python handler EFBIG handler ran, then raised 0

The first row is the one that surprises people: SIGXFSZ was already ignored in the environment those children started from, so nobody had to write a line of code to get the exception. That is convenient until it is not - it means the behaviour of a plain open() + write() depends on who launched the process.

This is where the language difference actually comes from. CPython sets SIGXFSZ (and SIGPIPE) to SIG_IGN in its own signal initialisation - signal_install_handlers() in Modules/signalmodule.c, called at interpreter start and called initsigs() in older versions:

#ifdef SIGXFSZ
PyOS_setsig(SIGXFSZ, SIG_IGN);
#endif

So a Python process starts with the signal ignored even if its launcher left the default, and a small C program that leaves the default in place dies. I checked the direction of that inheritance on the same host: a launcher that sets SIG_DFL and then execvs python3 produces a child in which signal.getsignal(SIGXFSZ) is already SIG_IGN. Set it back explicitly and the identical write returns status 153.

The practical lesson is not "use Python". It is that this behaviour is inherited configuration you did not write, so it can differ between your laptop, your container and your supervisor. Pin it.

Diagnose it in three checks

import os, resource, signal
print("disposition:", signal.getsignal(signal.SIGXFSZ)) # SIG_IGN = 1, SIG_DFL = 0
soft, hard = resource.getrlimit(resource.RLIMIT_FSIZE)
print("RLIMIT_FSIZE:", soft, hard, "(-1 = unlimited)")
row = [l for l in open("/proc/self/limits") if l.startswith("Max file size")]
print(row[0].strip())
  • The count a write returned. For an unbuffered write, a smaller number than you passed means the kernel clipped it and the file is at its ceiling. A buffered writer hides this until flush() / close(), which then raises - and close() may raise a second time, for the same bytes.
  • Max file size in /proc/<pid>/limits. It belongs to the process, not the filesystem, so staging and production can disagree while both disks are empty. Read it once and cache it: on this host a /proc read costs about 36 times a cached lookup.
  • Exit status 153. 128 + 25 is a signal death, not an exit code. Confirm with PPid and the unit or runtime above the process - a shell ulimit -f (which counts blocks, and the block size depends on the shell that parsed it), a systemd LimitFSIZE=, or a container runtime's --ulimit fsize=.

Three things that are not on that list, but look like it: truncate() and fallocate() are refused outright with EFBIG when their end offset is past the limit (no partial version, the file is left untouched); a write through an mmap mapping never takes this path, so the limit does not stop it; and pipes, sockets and /dev/null are not regular files, so the same 5,000-byte write that failed to a file succeeded to a pipe.

The fix I actually ship

Ignoring the signal at startup turns a disappearance into an exception your existing handler already reports. That is one line, it does not change the limit, and it is the first change I make:

import errno, signal
signal.signal(signal.SIGXFSZ, signal.SIG_IGN) # before the file is opened

def append_or_roll(path, chunk):
    with open(path, "ab") as f:
        f.write(chunk) # buffered: this may still "succeed"
        try:
            f.flush() # the bytes that fit land here
        except OSError as e:
            if e.errno != errno.EFBIG:
                raise
            return "rollover" # start file N+1, keep file N complete
        return "ok"

A file that genuinely must be larger than the hard limit needs another file (rollover, no privilege required) or an operator who can raise the limit - a process cannot raise its own hard limit. Raising the soft limit up to the hard one fixes a configuration mistake; it does not fix a design that outgrew the ceiling.

If the file already holds more bytes than the limit, that is expected: the limit gates growth, not current length, so writes wholly below the ceiling still succeed.

Checklist

  • [ ] Missing traceback + status 153 β†’ treat as SIGXFSZ, not OOM.
  • [ ] Truncated log ending at a round number β†’ check write counts, then flush() / close() errors.
  • [ ] Same code, different result in staging β†’ compare Max file size in both processes.
  • [ ] Want an exception instead of a death β†’ SIG_IGN at startup, explicitly.
  • [ ] Copy stopped part-way β†’ check the return of sendfile / copy_file_range, and shutil.copyfile, which raises after a partial file exists.
  • [ ] Writes to pipes too β†’ the limit is per process and regular-file only; it is shared by every thread.

This is the short version. The full article runs the five-limit boundary table, fourteen call types under the same ceiling, the cost of each probe, six commands that answer "where did this limit come from", and the complete probe source printed as it ran: File too large errno 27: RLIMIT_FSIZE, SIGXFSZ, and the bytes that fit.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.