-
Notifications
You must be signed in to change notification settings - Fork 97
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
Pyhtml sample code not working #84
Comments
Hi @krishnak,
|
Thank you for a quick feedback, While this is not directly related to this issue, I hope you can throw me some light on what needs to be done. I was trying to write a Captive Portal using your webserver on ESP32. I have re-built micropython with your WebServer as a module, your webserver works great !!. All good so far. I then came across this project which has already implemented captive portal using your code https://github.com/george-hawkins/micropython-wifi-setup I thought of reusing his code rather than reinventing the wheel, however he seems to have rewritten the /xasync_sockets.py HttpRequests etc. My use case is slightly different than a mere Captive Portal, I want clients to access ESP32 via both STA as well as from AP So can I just run two instances of the Webserver or is there a better optimised way to achieve this? |
Yes, it is possible. |
I tried the following code snippet
` `MPY: soft reboot
MWS2-INFO> Server listening on 192.168.4.1:80.
I started the server only on the AP as the WiFi settings for station mode is purposefully not configured. The server starts up. I get a forbidden error and then the server throws the exception as above. Please advise. |
@krishnak, could you check "station.ifconfig()[0]". |
That message is my debug statement from a function. `
MWS2-INFO> Server listening on 192.168.4.1:80.
|
Update: I had xasPool.AsyncWaitEvents(threadsCount=1) in the else block and also xasPool.AsyncWaitEvents(threadsCount=5) I have removed xasPool.AsyncWaitEvents(threadsCount=1) Now the server is stable on STA - no exceptions.page gets served normally as it does on the managed server. However the AP server still throws an exception |
Update 2: I have figured out the cause you need to give the solution. The exceptions are related to URI/files not being found by the server. The server on STA which is serving - crashes without exception (no longer accessible) if I try to access a non existent file. As the server on Access point is not serving any files (i.e not finding the files) it is throwing exceptions all the time. So please check whether you can reproduce this behaviour and decide whether its a bug. |
I've this code in
A found file is true if the size exists. |
Attached is the httpresponse.zip which is built Even if that is the case, why does the server running on AP not find the same file present in the root directory which the server on STA finds it. |
Hmm, since you managed to make it work on several threads, there may be an inter-thread blocking issue... I don't see at the moment. |
You can try to return a specific page on a "not found" page. But in my opinion it will crash out before the answer. |
I just found out that the browser is showing a cached page for the request to STA. I have shifted to wget which shows connection reset by peer for both STA as well as AP. I think there is some relation to number of threads and slot counts, I have reduced slot counts to 1 and it no longer works. What exactly does the slot count affect? I will try to install the firmware on another ESP32 tomorrow to see whether this is a hardware issue.
|
I have rebuilt your code with some print statements on XAsyncSockets before it throws the exception in OnReadyForReading It appears that there is empty line in the HTTP request and it throws error trying to decode it. see below actual line and decoded line
The last byte array is the one throwing error |
Ok @krishnak, this is a 'normal' raised exception in So, Here is the function :
The exception is catched here... Probably a memory problem when If you want, you can set
instead of :
|
Just @krishnak , you have probably reached the default thread stack size... |
Thanks for pointing the right function, I was assuming it was an error thrown at request side. I have missed out one issue on my side when I reflashed a recompiled version the firmware, I forgot to create WWW folder in the flash. Hence it wasn't finding the files. I have created this folder now, however the error still persists Now I am running only one server on STA with Startin Pool
However the behaviour is the same, if it finds the file it doesn't throw an exception, however it crashes before the full response is sent back- no further HTTP requests are possible after that. As you can see below there are two calls made to
The server side message is as follows, there is a 200 OK and then a 400 Bad request - I am not sure what is causing this , I think it is a threading issue (why the AsyncSendData is called twice?) when there is only one request., as the same hardware works in managed mode.
|
I have tested it again in ManagedPool it works as expected you can see the the server output below. I have found the issue it may not be memoryview, The onDataSent function inside httpResponse.py hangs at xasCli.AsyncSendSendingBuffer around line 155
|
Ok it actually is unable to get a lock here hence hangs
|
Actually its a simple fix, you need to put a thread.sleep before acquiring lock - there is a racing condition going on. I just added a print statement before acquire and that itself has given sufficient time for the threads to yield. With this print statement, I am now able to run both servers. However there is still an uncaught exception causing the lock to be held by a thread which has died. This results in the requests from other threads waiting for the lock
I think you can fix the code now. |
Ok @krishnak, If it works with several threads, it is because each thread has its memory while one thread does not reserve enough. Try to put these 2 lines in your main code, at the beginning :
|
I don't know python :) as deeply as you know. So I am happy to be enlightened. What I observed. If 404 error occurs, in AsyncPool mode - a new thread started to complain of 400 Bad request. So I thought there is another request, but it appears that 404 error thread never completed its task and has triggered 400 which got hung up. Your solution has fixed it 🥇 I have tested both the servers running on AP as well STA, they both are now working see attached logs. Working for both scenarios file being present and file being absent. No Crashes. I want to close this thread and thank you for resolving this issue. |
Well, I just didn't think about it before because I haven't used it for a while. And If you want, to create a captive portal during a Wi-Fi connection, you can use my little DNS server: |
Will Do look in to your DNS server thanks.
I need to look in to this as well
https://github.com/glenn20/micropython-espnow-images
…On Wed, 31 Aug 2022 at 16:16, Jean-Christophe Bos ***@***.***> wrote:
Well, I just didn't think about it before because I haven't used it for a
while.
And If you want, to create a captive portal during a Wi-Fi connection, you
can use my little DNS server:
https://github.com/jczic/MicroDNSSrv
(and set the default "404 not found" page to your root "/")
—
Reply to this email directly, view it on GitHub
<#84 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJTPN6NCFR4F6W47QODVBTV34ZYPANCNFSM6AAAAAAQAG42A4>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
When I set
The server crashes after first hit even for pages which are present, when I remove this line, everything works normally. Please check and advise. |
Also if xasPool.AsyncWaitEvents(threadsCount=2) if I increase this threadcount=3 - DNS Server doesn't start Can you please explain what is the slot count and what is thread count |
Just an update on the not found url, it works in managed mode. On AsyncPool it is crashing. The not found "logic" in your code is spawning many more threads in Async mode than what is spawned in Managed mode when a file is not found. I am judging this mainly from the number of thread lock messages that gets printed for a not found URL. in Managed mode the number of thread locks requested when a file is NOT FOUND is 10% of that of Async mode |
I think there is something specific happening with two socket operations involving the AP interface. I am able to run your microDNS server on STA interface along with 2 webservers (one each on AP and STA under managed mode) - however when I run the microDNS server or for that matter any two threads involving sockets on AP, only one thread has the CPU access at a time, this probably is the cause for this whole issue I have now tested this on 3 different hardwares to rule out hardware issue., |
I have found out the cause for the problem, on my Ubuntu machine when the WiFi network changes to connect to the ESP32's WiFi AP the Hotspot app opens up, this is resulting in an infinite number of calls to - this makes everything crash. However if I keep the connectivity checking switched OFF then everything works but there is some thing to investigate with the async thread implementation, The DNS server doesn't work for the same reason. I have written a different DNS server with poller and it works along with the HTTPServer.
|
With the Ubuntu auto connectivity switched OFF, the Startinpool works with two servers - no crashing. The crash is due to the code spawning too many threads/sockets when there is a not found scenario. This needs fixing. |
It complains about
TestVar not declared - I have tried setting TestVar in the main but it still complains. Can you provide a sample code for main
The text was updated successfully, but these errors were encountered: