Skip to content

Add support for Rename and Create events in inotify file watching#216

Open
JustinVenus wants to merge 1 commit intoaptible:mainfrom
JustinVenus:feature/inotify_support_rename_event
Open

Add support for Rename and Create events in inotify file watching#216
JustinVenus wants to merge 1 commit intoaptible:mainfrom
JustinVenus:feature/inotify_support_rename_event

Conversation

@JustinVenus
Copy link

Summary

This PR adds support for Rename and Create events in the inotify file watching implementation, fixing issue #204.

Problem

The current inotify implementation only handles Write and Remove events. This causes two main issues:

  1. Editor atomic writes: Editors like vim perform atomic writes by creating a temporary file and then renaming it over the original. This generates Rename events that were previously ignored, causing supercronic to miss file changes.

  2. Atomic file swaps: Users who want to atomically swap entire crontab configurations (e.g., by preparing a new directory and swapping it) cannot rely on supercronic detecting the new file at the watched path, as the swap operation generates Rename events.

Solution

This implementation:

  • Handles Rename events: When a rename is detected, the watch is re-added to the file path to ensure continued monitoring. This is critical because after a rename, the file descriptor may point to a different inode, but we want to continue watching the same path.

  • Handles Create events: Many editors use atomic writes that generate Create events. By handling these explicitly, we ensure all editor-based modifications are detected.

  • Improves Remove event handling: Makes error handling non-fatal for remove events (important for Kubernetes configmap/secret mounts where temporary removals are expected).

  • Maintains path-based watching: Unlike alternative solutions that would require watching entire directories, this solution continues to watch the specific file path, which is the preferred behavior according to the issue discussion.

Why This Solution?

This approach is preferred over alternatives because:

  1. Path-based watching: Users want to watch a specific path (preferably inside a directory) rather than an inode or entire directory. This solution maintains that behavior.

  2. Comprehensive event handling: By handling all relevant filesystem events (Write, Create, Rename, Remove), we cover all possible atomic write patterns used by editors and file management systems.

  3. Minimal changes: This solution extends the existing event handling logic without requiring architectural changes or additional dependencies (like fswatch).

  4. Kubernetes compatibility: The improved error handling ensures the solution works reliably with Kubernetes configmap and secret mounts, which use atomic write patterns.

Testing

The existing integration tests in integration/reload.bats already cover:

  • Write event handling
  • Kubernetes-like atomic writes using symlinks and folder deletion

This change extends that coverage to also handle:

  • Rename events (vim-style atomic writes)
  • Create events (other atomic write patterns)

Related Issues

Fixes aptible#204

Previously, the inotify watcher only handled Write and Remove events.
This caused issues when editors like vim perform atomic writes (which
generate Rename events) or when files are atomically swapped via
directory operations.

This change:
- Adds explicit handling for Rename events by re-adding the watch
  after rename operations (ensuring continued monitoring when files
  are replaced)
- Adds handling for Create events (which occur with atomic writes
  from some editors)
- Improves error handling for Remove events to be non-fatal (important
  for Kubernetes configmap/secret mounts)
- Maintains watching the specific file path rather than the directory
  or inode, as preferred by users

The solution watches the file path and handles all event types that
can occur during atomic file operations, making it robust for both
editor-based edits and atomic swap scenarios without requiring
directory-level watching.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] inotify support rename event (vim edit file)

1 participant