Skip to content

Conversation

@Fr3ya
Copy link

@Fr3ya Fr3ya commented Nov 21, 2025

When a TCP connection in eCONNECT_SYN state times out after exceeding the maximum retry count, the socket was incorrectly transitioning to eCLOSE_WAIT state instead of eCLOSED.

According to the standard TCP state machine (RFC 793):

  • eCONNECT_SYN is the state where a client has sent a SYN and is waiting for a SYN-ACK
  • When connection establishment fails (timeout or RST), the socket should transition to eCLOSED state

This fix:

  1. Changes the state transition in prvTCPSendPacket() from eCLOSE_WAIT to eCLOSED when SYN retries are exhausted
  2. Updates vTCPStateChange() to handle the eCONNECT_SYN -> eCLOSED transition, ensuring connection state change callbacks are properly triggered.

This also aligns the timeout behavior with the existing RST handling code, which correctly uses eCLOSED state (see FreeRTOS_TCP_IP.c:859).

Files changed:

  • source/FreeRTOS_TCP_Transmission.c: Fix state transition
  • source/FreeRTOS_TCP_IP.c: Update vTCPStateChange to handle eCLOSED transition from connecting states

Description

Test Steps

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

When a TCP connection in eCONNECT_SYN state times out after exceeding the maximum retry count, the socket was incorrectly transitioning to eCLOSE_WAIT state instead of eCLOSED.

According to the standard TCP state machine (RFC 793):
- eCONNECT_SYN is the state where a client has sent a SYN and is waiting for a SYN-ACK
- When connection establishment fails (timeout or RST), the socket should transition to eCLOSED state

This fix:
1. Changes the state transition in prvTCPSendPacket() from eCLOSE_WAIT to eCLOSED when SYN retries are exhausted
2. Updates vTCPStateChange() to handle the eCONNECT_SYN -> eCLOSED transition, ensuring connection state change callbacks are properly triggered.

This also aligns the timeout behavior with the existing RST handling code, which correctly uses eCLOSED state (see FreeRTOS_TCP_IP.c:859).

Files changed:
- source/FreeRTOS_TCP_Transmission.c: Fix state transition
- source/FreeRTOS_TCP_IP.c: Update vTCPStateChange to handle eCLOSED transition from connecting states
@htibosch
Copy link
Contributor

htibosch commented Dec 1, 2025

@Fr3ya , I have fixed the unit tests. I will send you an update later today.

@htibosch
Copy link
Contributor

htibosch commented Dec 2, 2025

@Fr3ya , I made some changes to repair the unit testing and 100% coverage :

ut_changes_htibosch.patch

./source/FreeRTOS_TCP_IP.c
./test/unit-test/FreeRTOS_TCP_IP/FreeRTOS_TCP_IP_utest.c

Can you apply my patch and do a new commit?

Here are the plain files in a zip:

ut_changes_htibosch.zip
Most changes are found in test_vTCPStateChange_ClosedWaitState_PrvStateSyn().

Copy link
Contributor

@htibosch htibosch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the changes, repaired the unit tests, and the 100% coverage. @Fr3ya , can you please apply the patch that I posted here?

@Fr3ya
Copy link
Author

Fr3ya commented Dec 5, 2025

Hi @htibosch, I have applied the patch that you posted, thanks!

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

Successfully merging this pull request may close these issues.

2 participants