Problems runnig atexit() function code properly - also under julia version 0.7.0-rc3/1.0.0-rc1 - OutOfMemoryError

question

#1

I cannot say where the evil comes from but when I like to use the atexit() function,
which you can found here in the docs I got these three kinds of error messages:

  1. ErrorException(“schedule: Task not runnable”)

  2. OutOfMemoryError
    atexit hook threw an error: OutOfMemoryError()

  3. ErrorException
    atexit hook threw an error: ErrorException(“schedule: Task not runnable”)

when running the following code in a script in background

#redirect stderr to put in a file later
orig_stderr = STDERR
(err_read, err_write) = redirect_stderr()

#do cleaning at exit
atexit(function()

      redirect_stderr(orig_stderr)
      println("Start cleaning ...")
      error_date = now()
      
      println("start error writing to file")
      close(err_write)
      error_name = string("/path_to_error_dir/",error_date)
      f = open(error_name,"w")
      write(f, readavailable(err_read))
      close(f)
      close(err_read)
     
      println("... finished.")

end)

#just sleep a little bit
function main1()

i = 0
while true
    i += 1
    println("start sleeping $i...")
    for j = 15:-1:1
        println("still $j")
        sleep(1)
    end
    println("stopped sleeping ...")
end

end

#observe a directory for changes in an interval of 15 seconds
function main2()

dir = "/path_to_observed_dir/"
i = 0
while true
    i += 1
    println("start observing round $i...")
    a = watch_file(dir,15)
    if a.changed
        println("changed")
    else
        println("status quo")
    end
    println("... stopped observing")
end

end

main1()
#or
#main2()

(where you have to replace

/path_to_error_dir/
/path_to_observed_dir/

with something real)

and interrupt the script with

kill -15 pid_of_process

with main1() I got in 50% of the runs error nr 1

and with main2() almost every time error 2 or 3.

WHAT I’m doing wrong? Or how can I avoid these errors?
Because they interrupt the cleaning in atexit() what is not desireable.
Which more information do you need to advise me?

Any help, hints or confirmation is very welcome.
As always, THX for reading and any helpful feedback.


#2

If I embed the content from atexit() in a try/catch construct as follow

try

content

catch

content

end

I ONLY get rid of error nr 1 but by the side effect that the whole content is repeated where a part may had finished already.


#3

I checked this problem under julia version 0.7.0-rc2
Error 1 is gone. But Error 2 and 3 still exists!

Therefore I precise the title a little bit.

The code for julia version 0.7.0-rc2 is now

using FileWatching
using Dates

#redirect stderr to put in a file later
orig_stderr = stderr
(err_read, err_write) = redirect_stderr()

#do cleaning at exit
atexit(function()

     redirect_stderr(orig_stderr)
     println("Start cleaning ...")
     error_date = now()
     
     println("start error writing to file")
     close(err_write)
     error_name = string("/path_to_error_dir/",error_date)
     f = open(error_name,"w")
     write(f, readavailable(err_read))
     close(f)
     close(err_read)
    
     println("... finished.")

end)

#observe a directory for changes in an interval of 15 seconds
function main()

   dir = "/path_to_observed_dir/"
   i = 0

   while true
       i += 1
       println("start observing round $i...")
       a = watch_file(dir,15)
       if a.changed
           println("changed")
       else
           println("status quo")
       end
       println("... stopped observing")
  end

end

main()

(where you have to replace

/path_to_error_dir/
/path_to_observed_dir/

with something real)

In 90% the cases I receive Error 2 and the last 10 % are occupied by Error 3.
I never get the expected behavior that the interruption error is written into file.

Any ideas, hints or explanations?
Thx for reading as always.


#4

Checked this against julia version 0.7.0-rc3 but still the same behavior.

Should I open a bug or is there something wrong which is adressable to me?

Thx for an advise. Happy juliacon :wink: