# How to make TCP socket listening more robust to errors?

**URL:** <https://discourse.julialang.org/t/how-to-make-tcp-socket-listening-more-robust-to-errors/38184>\
**Category:** General Usage\
**Created:** [April 25, 2020, 9:48am UTC](https://discourse.julialang.org/t/how-to-make-tcp-socket-listening-more-robust-to-errors/38184 "2020-04-25T09:48:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [April 25, 2020, 9:48am UTC](https://discourse.julialang.org/t/how-to-make-tcp-socket-listening-more-robust-to-errors/38184/1 "2020-04-25T09:48:19Z")

</div>

I’ve written a simple package for evaluating functions and passing data between Julia processes using TCP sockets.

It works pretty well, as long as the function evaluations don’t produce any errors. If the an error occurs, it won’t be printed and the listening socket breaks and needs to be restarted. The try/catch code I added doesn’t seem to be helping (no errors are ever caught).

Simplified version of the listening code:

```julia
function ipc_listen(addr::Sockets.InetAddr)
 server = Sockets.listen(addr)
 @async begin
  socket = Sockets.accept(server)
  while true
   try
    fun = Serialization.deserialize(socket)
    args = Serialization.deserialize(socket)
    @eval(Main, $fun($args...))
   catch e
    println(STDERR, e)
   end
 end
 server
end

```

Full code is here:

> **[GitHub - robsmith11/SimpleIPC.jl](https://github.com/robsmith11/SimpleIPC.jl)**
>
> Contribute to robsmith11/SimpleIPC.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [April 26, 2020, 6:52am UTC](https://discourse.julialang.org/t/how-to-make-tcp-socket-listening-more-robust-to-errors/38184/2 "2020-04-26T06:52:03Z")

</div>

The problem is that errors in an `@async` block are just swallowed if you leave the block dangling into nothing like that. You can either `wait` on the async block. Or you can do something similar to this code from my Animations package, where I tried to solve the same problem. I mostly copied it from some other thread once, but don’t remember which one…

```julia
macro async_showerr(ex)
    quote
        t = @async try
            eval($(esc(ex)))
        catch err
            bt = catch_backtrace()
            println("Asynchronous animation errored:")
            showerror(stderr, err, bt)
        end
    end
end

```

```julia
@async_showerr while true
    # do stuff
end

```
