From f4daee5ac308b889bd85a5318b2fea39588dae00 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 15 Sep 2026 20:07:44 -0700 Subject: [PATCH] espressif: retire the forward fd at every close Recording the direct forward's socket in the context left the two sites that close it on the target's own EOF or reset holding a stale copy, so the cleanup handler closed a descriptor number the task had since reissued. Every close now retires both the worker's copy and the context's, as the reference example does. - clear appFd where a zero read ends the forward - clear appFd and the worker's own fwdFd on a reset, which that branch never reset - compare appFd against a cast -1 in both places, since WS_SOCKET_T is unsigned on Windows --- .../ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c b/ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c index 9dab73e1..845b7c6d 100644 --- a/ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c +++ b/ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c @@ -1162,7 +1162,7 @@ static int ssh_worker(thread_ctx_t* threadCtx) * the id it retired. */ wolfSSH_GetLastRxId(ssh, &lastChannel); if (lastChannel == threadCtx->fwdCbCtx.channelId) { - if (threadCtx->fwdCbCtx.appFd == -1) { + if (threadCtx->fwdCbCtx.appFd == (WS_SOCKET_T)-1) { /* The cleanup handler ran ahead of this and * closed the socket; only this copy of the * descriptor is stale. */ @@ -1312,6 +1312,7 @@ static int ssh_worker(thread_ctx_t* threadCtx) to listening. */ WCLOSESOCKET(fwdFd); fwdFd = -1; + threadCtx->fwdCbCtx.appFd = -1; if (threadCtx->fwdCbCtx.hostName != NULL) { WFREE(threadCtx->fwdCbCtx.hostName, NULL, 0); @@ -1332,6 +1333,8 @@ static int ssh_worker(thread_ctx_t* threadCtx) /* Connection reset. Socket is closed. * Go back to listening. */ WCLOSESOCKET(fwdFd); + fwdFd = -1; + threadCtx->fwdCbCtx.appFd = -1; threadCtx->fwdCbCtx.state = FWD_STATE_LISTEN; continue; }