Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is luasocket thread safe? #175

Closed
tobiasschweizer opened this issue May 11, 2016 · 5 comments
Closed

Is luasocket thread safe? #175

tobiasschweizer opened this issue May 11, 2016 · 5 comments

Comments

@tobiasschweizer
Copy link

We are developing a software in C++ that makes use of Lua to easily implement some custom functionalities the user may wish to have. The software implements an API to request images in a specified resolution, rotation etc.

In https://github.com/dhlab-basel/Sipi/blob/develop/sipi.init-knora.lua, we implemented a Lua function that is called each time a client makes a request to our API. Each request is handled in a separate thread.

function pre_flight(prefix,identifier,cookie)

    -- use Sipi's luarocks to install these modules
    requests = require "requests"
    json = require "json"
...

We encountered a Lua problem when a lot of requests were made to our API at the same time. The application crashed. However, when we commented out requests = require "requests", all went fine again.

So could it be a problem that requests is loaded by separate threads at the same time? Each thread has its own Lua interpreter.

I asked the developer of lua-requests about this (JakobGreen/lua-requests#9) and he told me that his module depends on luasocket and that the problem could be there.

So I would like to ask you if luasocketis thread safe.

@moteus
Copy link
Contributor

moteus commented May 11, 2016

I think LuaSocket itself is thread safe. But some functions on some system not thread safe.
E.g. gethostbyaddr Is not thread safe on POSIX, but it thread safe for Windows. (may be need use gethostbyaddr_r).
I did not see any analize of all used functions in LuaSocket but you can take a look at this discussion

@diegonehab
Copy link
Contributor

LuaSocket is not thread safe because of buffering on the socket objects. If two threads attempt to read on the same socket, things may go very wrong. There is also the problem that Moteus mentioned. A few of the networking functions we use may or may not be thread safe depending on the OS. One possibility is to try to use the lua_lock and lua_unlock macros surrounding the critical regions to make sure everything is thread safe. Does anyone care to take a stab at it?

@tobiasschweizer
Copy link
Author

Thanks for your answers!

@diegonehab
Copy link
Contributor

Keep in mind that the default macros don't do anything. They must be redefined. LuaThreads used to do this, but there was so little interest that it was discontinued. The problem here is that Lua itself is not thread safe. So there can be only one script running on each Lua context at each time.

@sonoro1234
Copy link

Would it be thread safe for using two different socket connections, each one in a different lane of lualanes.?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants