python/cpython · error

cannot fstat '%s'

Error message

cannot fstat '%s'

What it means

Build-time error from Programs/_freeze_module.c: the input file opened successfully, but _Py_fstat_noraise() on its fd failed, so the freezer cannot learn the file size to allocate a buffer. The stream is closed and the freeze step aborts.

Source

Thrown at Programs/_freeze_module.c:96

        (void)PyInitConfig_GetError(config, &err_msg);
        printf("Python init error: %s\n", err_msg);
        PyInitConfig_Free(config);
        exit(1);
    }
}

static const char *
read_text(const char *inpath)
{
    FILE *infile = fopen(inpath, "rb");
    if (infile == NULL) {
        fprintf(stderr, "cannot open '%s' for reading\n", inpath);
        return NULL;
    }

    struct _Py_stat_struct stat;
    if (_Py_fstat_noraise(fileno(infile), &stat)) {
        fprintf(stderr, "cannot fstat '%s'\n", inpath);
        fclose(infile);
        return NULL;
    }
    size_t text_size = (size_t)stat.st_size;

    char *text = (char *) malloc(text_size + 1);
    if (text == NULL) {
        fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size);
        fclose(infile);
        return NULL;
    }
    size_t n = fread(text, 1, text_size, infile);
    fclose(infile);

    if (n < text_size) {
        fprintf(stderr, "read too short: got %ld instead of %ld bytes\n",
                (long) n, (long) text_size);
        free(text);

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Simply rerun the build — transient fstat failures on loaded machines usually clear
  2. Ensure no other job deletes or rewrites the input file during the build (serialize steps that generate it)
  3. Build on a local filesystem instead of NFS/SMB if metadata errors repeat
  4. Check for fd leaks / raise ulimit -n if builds fail at scale
Defensive patterns

Strategy: retry

Validate before calling

# shell: ensure inputs are present and stable before the build step
# [ -f "$IN" ] && [ ! -L "$IN" ] && stat "$IN" >/dev/null || exit 1

Prevention

When it happens

Trigger: The file at inpath is deleted or replaced between fopen() and fstat(); passing a path that resolves to something non-statable (e.g. /proc-like pseudo file on unusual filesystems, or a dangling symlink recreated mid-build); fd exhaustion in parallel builds.

Common situations: Concurrent build processes racing over the same generated files; antivirus or filesystem watchers on macOS/Windows transiently blocking stat; building on network filesystems (NFS/SMB) with flaky metadata.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/77ea1719e7542f96. Report an issue: GitHub.