FTP File Write Failure – Random ff_errno = 29 and HardFault Causing Device Restart

This issue occurs intermittently during the publishing of large-sized web pages and when transferring large files via FileZilla.

In our code, the condition if( xWritten != xRc ) is being triggered, indicating a mismatch between the number of bytes received and those successfully written to the file. This suggests a potential write failure during the file transfer.

Below is the relevant portion of the code from prvStoreFileWork():

static BaseType_t prvStoreFileWork( FTPClient_t *pxClient )
{
    BaseType_t xRc, xWritten;

    for( ; ; )
    {
        char *pcBuffer;

        // Zero-copy receive
        xRc = FreeRTOS_recv( pxClient->xTransferSocket, ( void * ) &pcBuffer,
                             0x20000u, FREERTOS_ZERO_COPY | FREERTOS_MSG_DONTWAIT );
        if( xRc <= 0 )
        {
            break;
        }

        pxClient->ulRecvBytes += xRc;
        xWritten = ff_fwrite( pcBuffer, 1, xRc, pxClient->pxWriteHandle );

        // Return buffer to the stack
        FreeRTOS_recv( pxClient->xTransferSocket, ( void * ) NULL, xRc, 0 );

        if( xWritten != xRc )
        {
            xRc = -1;
            pxClient->bits1.bHadError = pdTRUE_UNSIGNED;
            break;
        }
    }
    return xRc;
}

For further analysis, please refer to the attached Wireshark trace and the screenshots below: