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

Multiple http.get with individual URLs and callbacks broken or not supported? #1293

Closed
marcelstoer opened this issue May 13, 2016 · 2 comments

Comments

@marcelstoer
Copy link
Member

marcelstoer commented May 13, 2016

http://stackoverflow.com/q/37218528/131929 confused me and I ran a small test myself.

It appears to me as if we didn't support executing multiple http.get (or other HTTP functions) with individual URLs and callbacks. The below code makes two consecutive http.get calls. Each uses a separate URL and a specific callback function.

function wp(code, data)
    if (code < 0) then
      print("HTTP request to wp.me failed")
    else
      print("wp.me responded with HTTP status " .. code)
    end    
end

function nodemcu(code, data)
    if (code < 0) then
      print("HTTP request to nodemcu.com failed")
    else
      print("nodemcu.com responded with HTTP status " .. code)
    end
end

function run()
    http.get("http://wp.me/pzoQb-sk", nil, wp) -- returns HTTP 301
    http.get("http://nodemcu.com/favicon.png", nil, nodemcu) -- returns HTTP 200
end

What I would expect if run is invoked manually is "wp.me responded with HTTP status 301" and "nodemcu.com responded with HTTP status 200" in arbitrary order. What I get instead is a total mix-up:

=run()
nodemcu.com responded with HTTP status 301
=run()
nodemcu.com responded with HTTP status 301
=run()
nodemcu.com responded with HTTP status 200
=run()
nodemcu.com responded with HTTP status 200
=run()
nodemcu.com responded with HTTP status 301

This seems related to #1258.

If run is invoked automatically in a loop all requests fail:

for i=1,10 do run() end

HTTP request to wp.me failed
HTTP request to nodemcu.com failed
HTTP request to wp.me failed
HTTP request to nodemcu.com failed
HTTP request to wp.me failed
...

I hope this isn't an embarrassing case where I make a fool of myself because I miss something really obvious. Are we simply missing a few notes in the docs saying that you can have at most one such HTTP operation running at any given time?

@devsaurus
Copy link
Member

Quoting the docs:

Note that it is not possible to execute concurrent HTTP requests using this module. Starting a new request before the previous has completed will result in undefined behavior.

That's in line with the code, since the module can cope with only one single callback at a time.

@marcelstoer
Copy link
Member Author

Quoting the docs

Oh boy...I had missed that. I'll update the docs to make that a bit more explicit.

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

No branches or pull requests

2 participants