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

**URL:** https://discourse.julialang.org/t/problems-runnig-atexit-function-code-properly-also-under-julia-version-0-7-0-rc3-1-0-0-rc1-outofmemoryerror/12910
**Category:** General Usage
**Tags:** question, atexit
**Created:** [August 5, 2018, 7:33am UTC](https://discourse.julialang.org/t/problems-runnig-atexit-function-code-properly-also-under-julia-version-0-7-0-rc3-1-0-0-rc1-outofmemoryerror/12910 "2018-08-05T07:33:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![oniter](https://avatars.discourse-cdn.com/v4/letter/o/df705f/32.png) [@oniter](https://discourse.julialang.org/u/oniter)
#### Post date: [August 5, 2018, 7:33am UTC](https://discourse.julialang.org/t/problems-runnig-atexit-function-code-properly-also-under-julia-version-0-7-0-rc3-1-0-0-rc1-outofmemoryerror/12910/1 "2018-08-05T07:33:21Z")

</div>

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](https://docs.julialang.org/en/stable/stdlib/base/#Base.atexit) 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.

---

<div class="post-metadata">

### Author: ![oniter](https://avatars.discourse-cdn.com/v4/letter/o/df705f/32.png) [@oniter](https://discourse.julialang.org/u/oniter)
#### Post date: [August 5, 2018, 7:53am UTC](https://discourse.julialang.org/t/problems-runnig-atexit-function-code-properly-also-under-julia-version-0-7-0-rc3-1-0-0-rc1-outofmemoryerror/12910/2 "2018-08-05T07:53:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![oniter](https://avatars.discourse-cdn.com/v4/letter/o/df705f/32.png) [@oniter](https://discourse.julialang.org/u/oniter)
#### Post date: [August 6, 2018, 9:22am UTC](https://discourse.julialang.org/t/problems-runnig-atexit-function-code-properly-also-under-julia-version-0-7-0-rc3-1-0-0-rc1-outofmemoryerror/12910/3 "2018-08-06T09:22:16Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![oniter](https://avatars.discourse-cdn.com/v4/letter/o/df705f/32.png) [@oniter](https://discourse.julialang.org/u/oniter)
#### Post date: [August 7, 2018, 9:13pm UTC](https://discourse.julialang.org/t/problems-runnig-atexit-function-code-properly-also-under-julia-version-0-7-0-rc3-1-0-0-rc1-outofmemoryerror/12910/4 "2018-08-07T21:13:34Z")

</div>

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 😉
