yintao707 opened a new pull request, #18324:
URL: https://github.com/apache/nuttx/pull/18324

   ## Summary
   Previously, the server would add a new connection to the pending_list 
   and return POLLIN immediately after the server sent sock_sync (ns_ack).
   This caused a race condition where poll() returned POLLIN, but accept()
   would block waiting on sendsem until the client sent sock_sync.
   
   This change defers the connection notification until the client's 
   sock_sync message is received. Now:
   - Connection is added to pending_list in ept_cb when sock_sync arrives
   - POLLIN is returned only after the full handshake completes
   - accept() no longer blocks after poll() indicates readiness
   
   old
   ```
           Client                          Server                     User App
              |                               |                           |
              |-------- ns_create ----------->|                           |
              |                               |                           |
              |<------- ns_bind --------------|                           |
              |         (ns_ack)              |                           |
              |                               |                           |
              |                               |--[add to pending_list]--->|
              |                               |--[post recvsem]---------->|
              |                               |--[notify POLLIN]--------->|
              |                               |                           |
              |                               |                      poll() 
returns
              |                               |                           |
              |                               |                      accept() 
called
              |                               |                           |
              |                               |<----[wait sendsem]--------|  ← 
阻塞!
              |                               |                           |
              |-------- sock_sync ----------->|                           |
              |                               |--[post sendsem]---------->|
              |                               |                           |
              |<------- sock_sync ------------|                      accept() 
returns
              |                               |                           |
   ```
   
   new
   ```
           Client                          Server                     User App
              |                               |                           |
              |-------- ns_create ----------->|                           |
              |                               |                           |
              |<------- ns_bind --------------|                           |
              |         (ns_ack)              |                           |
              |                               |--[pending++]              |
              |                               |  (NOT add to list yet)    |
              |                               |                           |
              |-------- sock_sync ----------->|                           |
              |                               |                           |
              |                               |--[add to pending_list]--->|
              |                               |--[post recvsem]---------->|
              |                               |--[notify POLLIN]--------->|
              |                               |                           |
              |<------- sock_sync ------------|                      poll() 
returns
              |                               |                           |
              |                               |                      accept() 
called
              |                               |                           |
              |                               |   (sendsize > 0 ✓)        |
              |                               |                      accept() 
returns
              |                               |                           |
   ```
   
   ## Impact
   
   No impact on usage
   
   ## Testing
   
   sim:rpproxy/ sim:rpserver
   
   ```
   NuttShell (NSH) NuttX-12.12.0
   server> 
   server> rpsock_server stream block hello proxy &
   rpsock_server [9:100]
   server> server: create socket SOCK_STREAM nonblock 0
   server: bind cpu proxy, name hello ...
   server: listen ...
   server: try accept ...
   server> cu
   
   NuttShell (NSH) NuttX-12.12.0
   proxy> 
   proxy> 
   proxy> rpsock_client stream block hello server &
   rpsock_client [6:100]
   proxy> client: create socket SOCK_STREAM nonblock 0
   client: Connecting to server,hello...
   server: Connection accepted -- 4
   server: try accept ...
   client: Connected
   client send data, cnt 0, total len 64, BUFHEAD process0006, msg0000, 
name:hello
   client recv data process0006, msg0000, name:hello
   client recv total 64, send total 64
   client send data, cnt 1, total len 87, BUFHEAD process0006, msg0001, 
name:hello
   client recv data process0006, msg0001, name:hello
   ......
   client recv data, act len 4, total 4095996
   client recv done, total 4096000, endflags, send total 4096000
   server recv data normal exit
   server Complete ret 0, errno 0
   client: Terminating
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to