Skip to content

Read-write locks become None instead of raising when part of filelock fails to load #879

Closed

Description

@tobocop2

Problem

If part of filelock itself fails to load, the read-write lock classes silently become None instead of raising an error. filelock sets them to None on purpose when Python has no SQLite support, but the check cannot tell that case apart from a missing piece of filelock. The program then crashes later, at the first use of the lock, with an error that does not mention filelock.

This happens in frozen apps. Since 4.2, filelock loads the read-write lock code by name on first use, so freezers such as Nuitka and PyInstaller leave that code out unless told to include it.

Expected An import error that names the missing filelock module
Actual ReadWriteLock is None, then an unrelated TypeError where the program uses it

Suggested fix

Set the classes to None only when the missing module is SQLite, and raise every other import error:

    except ImportError as exc:  # pragma: lacks sqlite3
        if module_name not in {"._read_write", "._async_read_write"} or exc.name not in {"sqlite3", "_sqlite3"}:
            raise
        value = None

Environment

filelock 4.2.4, CPython 3.12, macOS 14.

Reproduction

The script hides the read-write lock modules, as a frozen app without them does. SQLite stays available.

import sqlite3  # SQLite is available
import sys


class _NotBundled:
    def find_spec(self, name, path=None, target=None):
        if name in {"filelock._read_write", "filelock._async_read_write"}:
            raise ModuleNotFoundError(f"No module named {name!r}", name=name)
        return None


sys.meta_path.insert(0, _NotBundled())

import filelock

print(filelock.__version__)        # 4.2.4
print(filelock.ReadWriteLock)      # None
print(filelock.AsyncReadWriteLock) # None
filelock.ReadWriteLock | None      # TypeError: unsupported operand type(s) for |

Activity

  1. changed the title [-]ReadWriteLock is None when its own module fails to import, not only without sqlite3[/-] [+]Read-write locks become None instead of raising when part of filelock fails to load[/+] on Oct 25, 2026
  2. added a commit that references this issue on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions