Using ioLibrary - What happens if close() on an already closed socket?

The source code for close() from the ioLibrary looks like this:
int8_t close(uint8_t sn)

    setSn_CR(sn, Sn_CR_CLOSE);
    /* wait to process the command... */
    while (getSn_CR(sn));
    /* clear all interrupt of the socket. */
    setSn_IR(sn, 0xFF);
    //A20150401 : Release the sock_io_mode of socket n.
    sock_io_mode &= ~(1 << sn);
    sock_is_sending &= ~(1 << sn);
    sock_remained_size[sn] = 0;
    sock_pack_info[sn] = 0;
    while (getSn_SR(sn) != SOCK_CLOSED);
    return SOCK_OK;

I am questioning the two while() statements in the code. Could they potentially hang the system? I am tracing the cause of a hung system down to the SPI driver, where it is waiting for replies from W5500 forever after close() is called. The bug doesn’t happen very often and seems to happen more frequent on “other people’s network”. Any thoughts?

My SPI driver has 10ms timeout for every byte it writes.

close must not hang the system, but it closes the socket, and another routine waiting socket to perform something may hang because that routine may not know that socket is closed and there’s nothing to expect from it.

You certainly need to find out which call is hanging, most probably you should not call it in case socket is closed.