From 47bbb47c52e0ce029b30a8d8c5d17478882f5a34 Mon Sep 17 00:00:00 2001
From: Tan Jay Jun <tan@jayjun.com>
Date: Wed, 30 Aug 2017 14:00:18 +0800
Subject: [PATCH] Kill inotifywait when BEAM process exits and stop leaks (#39)

---
 lib/file_system/backends/fs_inotify.ex | 5 +++--
 test/backends/fs_inotify_test.exs      | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/file_system/backends/fs_inotify.ex b/lib/file_system/backends/fs_inotify.ex
index 3077a91..9d4d9a4 100644
--- a/lib/file_system/backends/fs_inotify.ex
+++ b/lib/file_system/backends/fs_inotify.ex
@@ -106,9 +106,10 @@ defmodule FileSystem.Backends.FSInotify do
     {worker_pid, rest} = Keyword.pop(args, :worker_pid)
     case parse_options(rest) do
       {:ok, port_args} ->
+        bash_args = ['-c', to_charlist(executable_path()) ++ ' $0 $@ & PID=$!; read a; kill $PID']
         port = Port.open(
-          {:spawn_executable, to_charlist(executable_path())},
-          [:stream, :exit_status, {:line, 16384}, {:args, port_args}, {:cd, System.tmp_dir!()}]
+          {:spawn_executable, '/bin/sh'},
+          [:stream, :exit_status, {:line, 16384}, {:args, bash_args ++ port_args}, {:cd, System.tmp_dir!()}]
         )
         Process.link(port)
         Process.flag(:trap_exit, true)
diff --git a/test/backends/fs_inotify_test.exs b/test/backends/fs_inotify_test.exs
index 78b246c..a687aa4 100644
--- a/test/backends/fs_inotify_test.exs
+++ b/test/backends/fs_inotify_test.exs
@@ -24,7 +24,7 @@ defmodule FileSystem.Backends.FSInotifyTest do
       assert {:ok, ['-e', 'modify', '-e', 'close_write', '-e', 'moved_to', '-e', 'create', '-e',
                     'delete', '-e', 'attrib', '--format', [37, 119, 1, 37, 101, 1, 37, 102],
                     '--quiet', '-m', '/tmp']} ==
-        parse_options(dirs: ["/tmp"], recursive: false, unsuppported: :options)
+        parse_options(dirs: ["/tmp"], recursive: false, unsupported: :options)
     end
   end
 
-- 
GitLab