aosabook/500lines · error · OSError

inotify_add_watch() error: {}

Error message

inotify_add_watch() error: {}

What it means

Error "inotify_add_watch() error: {}" thrown in aosabook/500lines.

Source

Thrown at contingent/code/contingent/io.py:47

    changed_paths = []
    while not changed_paths:
        time.sleep(0.5)
        changed_paths = [path for path in paths
                         if os.stat(path).st_mtime > start]
    return changed_paths

def inotify_wait_on(paths):
    paths = [path.encode('ascii') for path in paths]
    fd = _libc.inotify_init()
    descriptors = {}
    if fd == -1:
        raise OSError('inotify_init() error: {}'.format(
            os.strerror(ctypes.get_errno())))
    try:
        for path in paths:
            rv = _libc.inotify_add_watch(fd, path, 0x2)
            if rv == -1:
                raise OSError('inotify_add_watch() error: {}'.format(
                    os.strerror(ctypes.get_errno())))
            descriptors[rv] = path
        buf = os.read(fd, 1024)
        # TODO: continue with some more reads with 0.1 second timeouts
        # to empty the list of roughly-simultaneous events before
        # closing our file descriptor and returning?
    finally:
        pass #os.close(fd)
    time.sleep(0.1)  # until above TODO is done
    wd, mask, cookie, name_length = struct.unpack('iIII', buf)
    return [descriptors[wd].decode('ascii')]

View on GitHub (pinned to fba689d101)

Solutions

  1. Ensure every path passed to inotify_wait_on exists and is readable before watching it.
  2. Increase fs.inotify.max_user_watches if the error is ENOSPC (watch limit reached).
  3. Verify paths encode to ASCII; non-ASCII paths must be encoded with the filesystem encoding instead of 'ascii'.

Example fix

sysctl fs.inotify.max_user_watches=524288  # and validate each path with os.path.exists(path) before adding the watch

When it happens

Trigger: Thrown at contingent/code/contingent/io.py:47 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of aosabook/500lines@fba689d101 (2026-08-13). Data as JSON: /api/errors/3def8b259d60dd1f. Report an issue: GitHub.