[GH-ISSUE #1296] Watcher fails to recognize new files #746

Closed
opened 2026-02-26 02:34:09 +03:00 by kerem · 3 comments
Owner

Originally created by @vitoyucepi on GitHub (Mar 26, 2021).
Original GitHub issue: https://github.com/koel/koel/issues/1296

Describe the bug
If I use inotifywait new files fail to add with error
Failed to synchronized /path/to/file.mp3. Maybe an invalid file?

To reproduce
Steps to reproduce the behavior:

  1. Use docs from https://docs.koel.dev/watch.html
  2. Add file to music library by using cp
  3. See error in watcher log

Expected behavior
New files are added by watcher without errors.

Screenshots

Environment

  • Koel version: 5.0.2
  • OS: archlinux
  • Browser: Chrome 89
  • PHP version: 7.4.16
  • Node version: N/A (because 5.0.2 is already compiled)

Additional context
I monitor events close_write, create, moved_to, delete and moved_from.
To do this I wrote the function

syncFile() {
#  sleep "${WATCHER_SLEEP_TIME:-0.5}"
  su www-data -s /bin/sh -c "php artisan koel:sync '${line}'"
}

monitorFiles() {
  newOrModifyEvents='close_write,create,moved_to'
  deleteEvents='delete,moved_from'
  inotifywait -rme "${newOrModifyEvents},${deleteEvents}" --format "%e %w%f" "${MEDIA_PATH}" |
    while read -r line; do
      file=$(echo "${line}" | cut -d ' ' -f2-)
      echo "${line}"
      if [ -e "${file}" ]; then
        if [ -d "${file}" ]; then
          chmod 0777 "${file}"
        else
          chmod 0666 "${file}"
#          stat -t "${file}"
#          file "${file}"
        fi
      fi
      syncFile
    done
}

For other commands like stat and file it works ok
Full log

watcher_1  | CREATE /music/music/2/9.mp3
watcher_1  | /music/music/2/9.mp3 10987268 21464 81b6 1000 1000 802 6424347 1 0 0 1616797827 1599835871 1616797829 0 4096
watcher_1  | /music/music/2/9.mp3: Audio file with ID3 version 2.3.0, contains:MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, JntStereo
watcher_1  | [2021-03-26 22:30:29] production.INFO: New watch record received: '/music/music/2/9.mp3'  
watcher_1  | [2021-03-26 22:30:29] production.INFO: '/music/music/2/9.mp3' is a file.  
watcher_1  | [2021-03-26 22:30:29] production.INFO: Failed to synchronized /music/music/2/9.mp3. Maybe an invalid file?  
watcher_1  | CLOSE_WRITE,CLOSE /music/music/2/9.mp3
watcher_1  | /music/music/2/9.mp3 10987268 21464 81b6 1000 1000 802 6424347 1 0 0 1616797829 1599835871 1616797829 0 4096
watcher_1  | /music/music/2/9.mp3: Audio file with ID3 version 2.3.0, contains:MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, JntStereo
watcher_1  | [2021-03-26 22:30:29] production.INFO: New watch record received: '/music/music/2/9.mp3'  
watcher_1  | [2021-03-26 22:30:29] production.INFO: '/music/music/2/9.mp3' is a file.  
watcher_1  | [2021-03-26 22:30:29] production.INFO: Failed to synchronized /music/music/2/9.mp3. Maybe an invalid file? 

If I add some delay before adding new file everything will be ok.

Maybe this problem exists because I use docker containers or maybe because of tmpfs.

Originally created by @vitoyucepi on GitHub (Mar 26, 2021). Original GitHub issue: https://github.com/koel/koel/issues/1296 **Describe the bug** If I use `inotifywait` new files fail to add with error `Failed to synchronized /path/to/file.mp3. Maybe an invalid file?` **To reproduce** Steps to reproduce the behavior: 1. Use docs from https://docs.koel.dev/watch.html 2. Add file to music library by using `cp` 4. See error in watcher log **Expected behavior** New files are added by watcher without errors. **Screenshots** **Environment** - Koel version: 5.0.2 - OS: archlinux - Browser: Chrome 89 - PHP version: 7.4.16 - Node version: N/A (because 5.0.2 is already compiled) **Additional context** I monitor events `close_write`, `create`, `moved_to`, `delete` and `moved_from`. To do this I wrote the function ```sh syncFile() { # sleep "${WATCHER_SLEEP_TIME:-0.5}" su www-data -s /bin/sh -c "php artisan koel:sync '${line}'" } monitorFiles() { newOrModifyEvents='close_write,create,moved_to' deleteEvents='delete,moved_from' inotifywait -rme "${newOrModifyEvents},${deleteEvents}" --format "%e %w%f" "${MEDIA_PATH}" | while read -r line; do file=$(echo "${line}" | cut -d ' ' -f2-) echo "${line}" if [ -e "${file}" ]; then if [ -d "${file}" ]; then chmod 0777 "${file}" else chmod 0666 "${file}" # stat -t "${file}" # file "${file}" fi fi syncFile done } ``` For other commands like `stat` and `file` it works ok Full log ``` watcher_1 | CREATE /music/music/2/9.mp3 watcher_1 | /music/music/2/9.mp3 10987268 21464 81b6 1000 1000 802 6424347 1 0 0 1616797827 1599835871 1616797829 0 4096 watcher_1 | /music/music/2/9.mp3: Audio file with ID3 version 2.3.0, contains:MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, JntStereo watcher_1 | [2021-03-26 22:30:29] production.INFO: New watch record received: '/music/music/2/9.mp3' watcher_1 | [2021-03-26 22:30:29] production.INFO: '/music/music/2/9.mp3' is a file. watcher_1 | [2021-03-26 22:30:29] production.INFO: Failed to synchronized /music/music/2/9.mp3. Maybe an invalid file? watcher_1 | CLOSE_WRITE,CLOSE /music/music/2/9.mp3 watcher_1 | /music/music/2/9.mp3 10987268 21464 81b6 1000 1000 802 6424347 1 0 0 1616797829 1599835871 1616797829 0 4096 watcher_1 | /music/music/2/9.mp3: Audio file with ID3 version 2.3.0, contains:MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, JntStereo watcher_1 | [2021-03-26 22:30:29] production.INFO: New watch record received: '/music/music/2/9.mp3' watcher_1 | [2021-03-26 22:30:29] production.INFO: '/music/music/2/9.mp3' is a file. watcher_1 | [2021-03-26 22:30:29] production.INFO: Failed to synchronized /music/music/2/9.mp3. Maybe an invalid file? ``` If I add some delay before adding new file everything will be ok. Maybe this problem exists because I use docker containers or maybe because of tmpfs.
kerem closed this issue 2026-02-26 02:34:09 +03:00
Author
Owner

@phanan commented on GitHub (Mar 29, 2021):

TBH I'm not familiar with inotify-tools's internals, especially not within the Archlinux context. Judging from the log and what you described, looks like the event is fired before the file handler is closed, hence the issue. If a short delay fixes the issue for you, I'd suggest going with that.

<!-- gh-comment-id:809461795 --> @phanan commented on GitHub (Mar 29, 2021): TBH I'm not familiar with `inotify-tools`'s internals, especially not within the Archlinux context. Judging from the log and what you described, looks like the event is fired before the file handler is closed, hence the issue. If a short delay fixes the issue for you, I'd suggest going with that.
Author
Owner

@vitoyucepi commented on GitHub (Mar 29, 2021):

I've already created container images for that purpose.
They use php-apache based on ubuntu.

Will it be easy for you to reproduce this issue locally if you use my images. Or maybe you want to build them from the source?

<!-- gh-comment-id:809471237 --> @vitoyucepi commented on GitHub (Mar 29, 2021): I've already created container images for that purpose. They use php-apache based on ubuntu. Will it be easy for you to reproduce this issue locally if you use my images. Or maybe you want to build them from the source?
Author
Owner

@vitoyucepi commented on GitHub (Mar 30, 2021):

Maybe add some debug message here?
What kind of error.
github.com/koel/koel@0250970de0/app/Services/MediaSyncService.php (L255)

Or return $result as either. It can be only in one state. ok with value or error with error(s).
So in the error dispatcher we can check error type and do some logging.

<!-- gh-comment-id:809945128 --> @vitoyucepi commented on GitHub (Mar 30, 2021): Maybe add some debug message here? What kind of error. https://github.com/koel/koel/blob/0250970de0d7494b1c0c1e07bc61c4854de1157b/app/Services/MediaSyncService.php#L255 Or return `$result` as either. It can be only in one state. `ok` with value or `error` with error(s). So in the error dispatcher we can check error type and do some logging.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/koel-koel#746
No description provided.