I use luasocket and copas to run the chat server. You need to free the port (i.e. server:close() ) when the program ends. How can you perform this task without using C, if possible?

Attempts to use the module did not help, because the server finalizer was invoked before client finalization (I also disable them as finalizers).

    1 answer 1

    Obviously, just finalizers are not called. To exit correctly, use return in the main function. In other words, make a check on the exit condition, add using copas.addthread . It is important that the function does not block the loop, and after checking it copas.sleep() . Instead of copas.loop then use

     while not exitnow do copas.step() end 

    UPD: or use os.exit(exitcode, true) .