How to make TCP socket listening more robust to errors?

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…

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
@async_showerr while true
    # do stuff
end
1 Like