Index: sys/kern/uipc_usrreq.c =================================================================== --- sys/kern/uipc_usrreq.c +++ sys/kern/uipc_usrreq.c @@ -1382,10 +1382,23 @@ goto bad2; } if (so->so_proto->pr_flags & PR_CONNREQUIRED) { + + unp2 = sotounpcb(so2); if (so2->so_options & SO_ACCEPTCONN) { - CURVNET_SET(so2->so_vnet); - so3 = sonewconn(so2, 0); - CURVNET_RESTORE(); + + /* Ref count for unp2 of so2, before we drop the lock so that we + * dont loose unp2 from under in uipc_detach. + */ + unp2->unp_refcount++; + UNP_LINK_WUNLOCK(); + CURVNET_SET(so2->so_vnet); + so3 = sonewconn(so2, 0); + CURVNET_RESTORE(); + UNP_LINK_WLOCK(); + unp2->unp_refcount--; + /* make sure v_socket is stable as we dropped the lock earlier. */ + VOP_UNP_CONNECT(vp, &so2); + } else so3 = NULL; if (so3 == NULL) { @@ -1393,7 +1406,6 @@ goto bad2; } unp = sotounpcb(so); - unp2 = sotounpcb(so2); unp3 = sotounpcb(so3); UNP_PCB_LOCK(unp); UNP_PCB_LOCK(unp2);