Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork217
SocketWrapper MbedClient debugging readSocket#912
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
andreagilardoni commentedJul 1, 2024
Hi@JAndrassy, I agree with the changes you made for point 1. |
JAndrassy commentedJul 1, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@andreagilardoni so should we add everywhere to
I didn't look in the source but I think the unconnected socket still counts as used one from socket-max |
andreagilardoni commentedJul 1, 2024
I think the only method missing the check for The changes on lines 123:124 are necessary. I only need to understand why you performed changes on |
JAndrassy commentedJul 1, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
|
00b5add toa5fc13fCompareJAndrassy commentedJul 2, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
changes:
|
444339a to00bc011CompareUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
andreagilardoni commentedJul 2, 2024
@JAndrassy after looking at that everything seems ok, I need to try to run some examples and then I think we can merge this PR. I like how you reorganized the thread function, thanks for your contribution! |
00bc011 toa40c63fCompareJAndrassy commentedJul 2, 2024
There is one more problem with the readSocket thread, but if I patch the solution in, it screams for a rewrite of the whole inner loop. the problem is that on err < 0 the thread ends. it can be just that the peer closed the connection. on a new |
andreagilardoni commentedJul 3, 2024
I quickly tested this PR and I don't see any issues with this. About the other issue you are describing, what |
JAndrassy commentedJul 3, 2024
sorry ret not err |
andreagilardoni commentedJul 3, 2024
If the peer closed the connection than, I think, it is expected that the socket has to be closed and needs to be restarted. In connect I can see that this check is performed ArduinoCore-mbed/libraries/SocketWrapper/src/MbedClient.cpp Lines 84 to 89 in2ece915
Did I get your point? |
JAndrassy commentedJul 3, 2024
then it is ok |
maidnl left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I tried to analyse and test the proposed changes and I have a few comments (I hope you can find them meaningful).
One thing I found really confusing is the use of the_status flag which is set / reset in a lot of different places (and this make difficult to understand the underling logic).
In thereadSocket() thread is appears that is not really necessary to set_status=true at the end of each innermostdo-while cicle.
There is no need to set_status=true after the call of theconfigureSocket() function since is theconfigureSocket() function itself sets_status=true;. This happens twice: in theconnect() and theconnectSSL() function.
As additional simplification I would reset_status to false at the beginning of those 2 functions (if all goes well theconfigureSocket() function will set it totrue and in case of problem it will remain set tofalse removing the need of setting if tofalse in case of errors). This removes the need to set_status to false in case of problem and ensure it is false if something wrong happened.
About the mutex lock/unlock in thewrite() function, my understanding is that the mutex is used to prevent access to therxBuffer from different threads, so it appears to be not necessary here. One point that is certainly wrong is that the changes removed the check about thesock pointer: it is necessary to reintroduce here the check
if (sock == nullptr) { return 0;}this is very important because a user can callwrite() with an invalidsock and this would crash the program.
Since the mutex is used to prevent "common" access torxBuffer I noticed that thepeek() function that usesrxBuffer is unprotected: I would add the mutex lock/unlock mechanism to thepeek() function.
A possible improvement is related to thesetSocket() function: this function is called when client is create by a server. However the server allocates aSocket only if there is an incoming request, in case no request is made the server will always callsetSocket(nullptr) (please check theEthernetServer::available() function in the EthernetServer.cpp file and verify if my understanding is correct).
In case like this it is probably pointless to callconfigureSocket() so I would add a check and execute the body of the function only if_sock is different from nullptr.
My last remark is more a doubt: in theconnect() andconnectSSL() function almost at the end of the function in caseret is not 1 it has been added the statementsock->close(), but this only closes theSocket. Would not be better to call directly theMbedClientstop() function? This would reset all the variables held by the client and not only "close the socket".
Uh oh!
There was an error while loading.Please reload this page.
JAndrassy commentedJul 24, 2024
I avoid doing unnecessary changes in my PR. Where would I stop if I begin to cleanup the code. So I don't even start. So the superfluous The readSocket() function runs in a separate thread so it can't switch _status to false if it isn't definitive. I can remove the lock from write. (btw if socket is null, then mutex is null too) yes. peek() needs the lock. I add it.
as I understand it, the idea is that the socket can be reused for next try to connect, that is why it is not deleted. calling stop() would delete it. all other fields are not initialized because configureSocket didn't run |
a40c63f todbe0b07Comparedbe0b07 to3428d75CompareJAndrassy commentedJul 25, 2024
I made these changes ^^^ |
facchinm commentedSep 5, 2024
Maybe this patch could help fixing#937 ? |
| do { | ||
| mutex->lock(); | ||
| if (sock !=nullptr && rxBuffer.availableForStore() ==0) { | ||
| if (sock ==nullptr) { |
schnoberts1Sep 9, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Is this subject to the same issues as "Another racy example" here:https://queue.acm.org/detail.cfm?id=2088916? sock is loop invariant but changed in another thread. mutex is the same isn't it?
ppp-one commentedJun 20, 2025
Just throwing#1067 here. Queries to the Arduino Opta hang for ~100ms due to |
Uh oh!
There was an error while loading.Please reload this page.
In
connectwe have to delete the socket object, because all other functions test it for null and then use mutex which is null, because configureSocket was not invoked. And there are too few sockets in total.In
readSocketthe condition of the inner loop was always true and the inner loop never exited to wait for event or timeout on event. The inner loop only paused for yield(). With the PR if no data are available the inner loop doesbreakto outer loop where the thread waits for event or timeout.3) I think we have to do mutex inwritetoo