CC: Tweaked

CC: Tweaked

57M Downloads

Make http.get coroutine safe

kepler155c opened this issue ยท 3 comments

commented

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
)

screenshot from 2019-02-06 17-58-01

commented

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

commented

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.

commented

I ran into this working on skynet, it's quite annoying but can be worked around at least.