Make http.get coroutine safe
kepler155c opened this issue ยท 3 comments
Calling http.get within different coroutines for the same URL will give unexpected results.
http.get will return the same file handle to both. When closed by the first routine, the second routine will error.
parallel.waitForAll(
function()
local h = http.get('https://pastebin.com/raw/YPWgiFFW')
if h then
local c = h.readAll()
h.close()
print('fn 1')
print(c and #c or 'nil')
end
end,
function()
local h = http.get('https://pastebin.com/raw/YPWgiFFW')
if h then
local c = h.readAll()
h.close()
print('fn 2')
print(c and #c or 'nil')
end
end
)
For now, you could mitigate this by adding a cache buster to your URLs, a random string at the end with a URL fragment on each request like #abcd on one and #1234 on another
http_success
/http_failure
identifies the events via their URLs, meaning that http.get
/http.post
cannot distinguish between requests, and so just return on the first one.
The obvious solution here is to use numeric IDs, like os.startTimer
/os.startAlarm
. However, the only backwards compatible way is to chuck this on the very end, which is a tad grot - hence my reluctance to do this so far.
As Lem says, the workaround is just to make your URLs unique with some guff which doesn't actually matter.