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

Set socket to MAX_SOCK_NUM if connection fails #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

goccert25
Copy link

@goccert25 goccert25 commented Nov 8, 2022

I've been experiencing a bug using the Arduino to communicate over ethernet when using multiple EthernetClients, and as far as I can tell this line is the culprit. The exact steps of how the bug happens:

  • Initialize two EthernetClients A and B to talk to HOST at PORT1 and PORT2
  • A.connect(HOST, PORT1); is called first. Inside the connect function, the function socketBegin is called, which according to the code here finds the first unused socket number and returns it. Lets say in this case the first unused socket number is 0. This means that A._socketIndex ends up being 0.
  • For some reason, A.connect(HOST, PORT1); fails on line 63. However, note that A._socketIndex is NOT reset back to MAX_SOCK_NUM and stays at the initialized value (for this example we'll stick with 0).
  • B.connect(HOST, PORT2); is called second. The process as above happens, and B._socketIndex is set to 0. However, for some reason B.connect(HOST, PORT2); works, and B is able to connect.
  • Note that now B._socketIndex == A._socketIndex == 0. However, since A.connect(HOST, PORT1) returned 0 (which stands for false), calls to A.available(), A.read() shouldn't work. Instead, they do work, and even worse A.write() will write data to the socket that B is connected to.

This change should fix the bug, because instead of returning 0 on line 63 we break, we set _socketIndex back to MAX_SOCK_NUM and everything behaves as expected.

@goccert25 goccert25 changed the title Set socket to invalid socket if connection fails Set socket to MAX_SOCK_NUM if connection fails Nov 8, 2022
@goccert25
Copy link
Author

@karlsoderby @per1234 , this is my first time contributing to the Arduino ecosystem. Please let me know if there's anything missing from the PR or anything else you'd like to see from me :)

@github-actions
Copy link

github-actions bot commented Nov 8, 2022

Memory usage change @ ec1ed04

Board flash % RAM for global variables %
arduino:avr:leonardo 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:avr:mega 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:avr:nano 💚 -2 - 0 -0.01 - 0.0 0 - 0 0.0 - 0.0
arduino:megaavr:nona4809 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:megaavr:uno2018 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:sam:arduino_due_x_dbg 🔺 0 - +8 0.0 - 0.0 N/A N/A
arduino:samd:arduino_zero_edbg 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkr1000 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrfox1200 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrzero 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table
Board examples/AdvancedChatServer
flash
% examples/AdvancedChatServer
RAM for global variables
% examples/BarometricPressureWebServer
flash
% examples/BarometricPressureWebServer
RAM for global variables
% examples/ChatServer
flash
% examples/ChatServer
RAM for global variables
% examples/DhcpAddressPrinter
flash
% examples/DhcpAddressPrinter
RAM for global variables
% examples/DhcpChatServer
flash
% examples/DhcpChatServer
RAM for global variables
% examples/LinkStatus
flash
% examples/LinkStatus
RAM for global variables
% examples/PagerServer
flash
% examples/PagerServer
RAM for global variables
% examples/TelnetClient
flash
% examples/TelnetClient
RAM for global variables
% examples/UDPSendReceiveString
flash
% examples/UDPSendReceiveString
RAM for global variables
% examples/UdpNtpClient
flash
% examples/UdpNtpClient
RAM for global variables
% examples/WebClient
flash
% examples/WebClient
RAM for global variables
% examples/WebClientRepeating
flash
% examples/WebClientRepeating
RAM for global variables
% examples/WebServer
flash
% examples/WebServer
RAM for global variables
%
arduino:avr:leonardo 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:avr:mega 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:avr:nano -2 -0.01 0 0.0 -2 -0.01 0 0.0 -2 -0.01 0 0.0 0 0.0 0 0.0 -2 -0.01 0 0.0 0 0.0 0 0.0 -2 -0.01 0 0.0 -2 -0.01 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -2 -0.01 0 0.0 -2 -0.01 0 0.0 -2 -0.01 0 0.0
arduino:megaavr:nona4809 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:megaavr:uno2018 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0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0 0.0 4 0.0 0 0.0 4 0.0 0 0.0
Click for full report CSV
Board,examples/AdvancedChatServer<br>flash,%,examples/AdvancedChatServer<br>RAM for global variables,%,examples/BarometricPressureWebServer<br>flash,%,examples/BarometricPressureWebServer<br>RAM for global variables,%,examples/ChatServer<br>flash,%,examples/ChatServer<br>RAM for global variables,%,examples/DhcpAddressPrinter<br>flash,%,examples/DhcpAddressPrinter<br>RAM for global variables,%,examples/DhcpChatServer<br>flash,%,examples/DhcpChatServer<br>RAM for global variables,%,examples/LinkStatus<br>flash,%,examples/LinkStatus<br>RAM for global variables,%,examples/PagerServer<br>flash,%,examples/PagerServer<br>RAM for global variables,%,examples/TelnetClient<br>flash,%,examples/TelnetClient<br>RAM for global variables,%,examples/UDPSendReceiveString<br>flash,%,examples/UDPSendReceiveString<br>RAM for global variables,%,examples/UdpNtpClient<br>flash,%,examples/UdpNtpClient<br>RAM for global variables,%,examples/WebClient<br>flash,%,examples/WebClient<br>RAM for global variables,%,examples/WebClientRepeating<br>flash,%,examples/WebClientRepeating<br>RAM for global variables,%,examples/WebServer<br>flash,%,examples/WebServer<br>RAM for global variables,%
arduino:avr:leonardo,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:avr:mega,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:avr:nano,-2,-0.01,0,0.0,-2,-0.01,0,0.0,-2,-0.01,0,0.0,0,0.0,0,0.0,-2,-0.01,0,0.0,0,0.0,0,0.0,-2,-0.01,0,0.0,-2,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,0.0,4,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0
arduino:samd:mkrzero,4,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0
arduino:samd:nano_33_iot,4,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0,4,0.0,0,0.0

@goccert25
Copy link
Author

Hey @karlsoderby @per1234, bumping this. Is there anyway to move this PR forward?

@per1234 per1234 added topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project labels Dec 17, 2022
@per1234
Copy link
Contributor

per1234 commented Dec 17, 2022

There is an alternative proposal at #176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants