Bug 3538

Summary: Sshd reported error:Bind to port 22 failed because of Address already in use
Product: Portable OpenSSH Reporter: renmingshuai <rmsh1216>
Component: sshdAssignee: Assigned to nobody <unassigned-bugs>
Status: NEW ---    
Severity: enhancement CC: djm
Priority: P1    
Version: 9.1p1   
Hardware: Other   
OS: Linux   

Description renmingshuai 2023-02-13 14:05:03 AEDT
When I run "systemctl restart sshd", the sshd service reported an error: Bind to port 22 failed because of Address already in use. Why the address is in use in this case is that the close() system call is asynchronous. The kernel implementation of close is to put the real close work in a work queue and wait for the system to schedule. When the system is busy, closing the socket may be delayed, and restarting the sshd service would cause this error.

the call stack of close() is as shown below

close(fd) // sshd calls close()
    SYSCALL_DEFINE1(close, unsigned int, fd); // kernel code
        __close_fd(current->files, fd);
            filp_close(filp,id);
                fput(filp); // put the next work in an work queue
                return retval; // return without confirming


I don't know if it's a problem and if it is, how to solve it?
Comment 1 Damien Miller 2023-02-22 15:10:21 AEDT
that really sounds like a bug in the kernel. The sequence of close+open should produce sensible results regardless of the kernel internals. I'd be surprised if sshd was the only thing affected by this.