Skip to content

Re-check a refused FIN on later segments - #1386

Open
rgezikov wants to merge 1 commit into
FreeRTOS:mainfrom
rgezikov:fix/recheck-refused-fin
Open

rgezikov wants to merge 1 commit into
FreeRTOS:mainfrom
rgezikov:fix/recheck-refused-fin

Conversation

@rgezikov

@rgezikov rgezikov commented Nov 7, 2026

Copy link
Copy Markdown

Description

When the peer's FIN arrives while the socket still has Tx data queued, prvHandleEstablished()
refuses it (the "Refusing FIN" path with bTxDone == 0). Since #1195 it also advances
rx.ulCurrentSequenceNumber past the FIN, so the FIN is ACKed and the peer will not
retransmit it.

The FIN check, however, only runs when bFinAccepted is set or the current segment carries
FIN. The peer's later segments only ACK our remaining data, so the refused FIN is never
looked at again. Once the Tx queue drains, the socket stays ESTABLISHED forever:

  • it never sends its own FIN, and the peer sits in FIN_WAIT_2;
  • FreeRTOS_recv() never returns, so a task blocked in it waits indefinitely.

This change also enters the check when bFinRecv is set. A refused FIN is then re-evaluated
on each later segment, until Rx and Tx are complete and the normal closing flow
(prvTCPHandleFin()) runs.

Test Steps

Hardware, V4.4.2: NUCLEO-F767ZI (STM32F767ZI), STM32 network interface,
FreeRTOS-Kernel V11.1.0.

  • The board runs a TCP echo server: accept, then FreeRTOS_recv()/FreeRTOS_send() until
    recv returns <= 0, then shutdown and close.

  • A Linux PC sends 100,000 bytes and half-closes right away, so the PC's FIN arrives while
    the echo is still being transmitted:

    s = socket.create_connection((board, 7))
    s.sendall(b"x" * 100_000)
    s.shutdown(socket.SHUT_WR)
    while s.recv(65536): pass

Results:

Build 3 runs
Unchanged V4.4.2 All data is echoed, but the board's FIN never arrives. After 15 s the PC's socket is still in FIN-WAIT-2 (ss -tan). The echo task stays blocked in FreeRTOS_recv(), so the next two connections are never served.
With this change 3/3 pass: all data is echoed, the board's FIN follows immediately, and the PC's socket goes to TIME-WAIT.

Unit tests: two new tests in FreeRTOS_TCP_State_Handling_utest.c.

  • test_prvHandleEstablished_FINRecvRefusedEarlierThenTXDone: an ACK without FIN, after a
    refused FIN, with Rx and Tx done, accepts the FIN and starts the closing flow.
  • test_prvHandleEstablished_FINRecvRefusedEarlierTXNotDone: the same while Tx data is still
    pending refuses it again and only ACKs.

Both fail on the current main and pass with this change. The full unit-test suite passes:
ctest -E system, 60/60.

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

Follow-up to #1195 (which made the refused FIN ACKed). No separate issue filed.

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

When the peer's FIN arrives while Tx data is still queued,
prvHandleEstablished() refuses it, but since FreeRTOS#1195 it advances
rx.ulCurrentSequenceNumber past the FIN, so the FIN is ACKed and the
peer will not retransmit it. The FIN check only ran when bFinAccepted
was set or the current segment carried FIN, so the refused FIN was never
looked at again: once the Tx queue drained, the socket stayed
ESTABLISHED, never sent its own FIN, and FreeRTOS_recv() never returned.

Also enter the check when bFinRecv is set, so the refused FIN is
re-evaluated on each later segment until Rx and Tx are complete.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KSfhgGX3d6cx2tfP6Leo4S

This branch has not been deployed

No deployments
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.

1 participant