[core] Remove pre-sleep socket scan from fast select path (#15639)

This commit is contained in:
J. Nick Koston
2026-04-21 04:40:03 +02:00
committed by GitHub
parent 0d3a3552da
commit a5b1f3eece
7 changed files with 96 additions and 111 deletions
+20 -24
View File
@@ -14,38 +14,34 @@ BSDSocketImpl::BSDSocketImpl(int fd, bool monitor_loop) {
if (!monitor_loop || this->fd_ < 0)
return;
#ifdef USE_LWIP_FAST_SELECT
// Cache lwip_sock pointer and register for monitoring (hooks callback internally)
this->cached_sock_ = esphome_lwip_get_sock(this->fd_);
this->loop_monitored_ = App.register_socket(this->cached_sock_);
this->cached_sock_ = hook_fd_for_fast_select(this->fd_);
#else
this->loop_monitored_ = App.register_socket_fd(this->fd_);
#endif
}
BSDSocketImpl::~BSDSocketImpl() {
if (!this->closed_) {
this->close();
}
}
BSDSocketImpl::~BSDSocketImpl() { this->close(); }
int BSDSocketImpl::close() {
if (!this->closed_) {
// Unregister before closing to avoid dangling pointer in monitored set
#ifdef USE_LWIP_FAST_SELECT
if (this->loop_monitored_) {
App.unregister_socket(this->cached_sock_);
this->cached_sock_ = nullptr;
}
#else
if (this->loop_monitored_) {
App.unregister_socket_fd(this->fd_);
}
#endif
int ret = ::close(this->fd_);
this->closed_ = true;
return ret;
if (this->fd_ < 0) {
// Already closed, or never opened.
return 0;
}
return 0;
#ifdef USE_LWIP_FAST_SELECT
// Null the cached lwip_sock pointer before closing. The underlying lwip slot can be
// recycled for a new connection as soon as ::close() returns, so anything that might
// dereference cached_sock_ post-close (e.g. setsockopt(TCP_NODELAY)) would otherwise
// touch an unrelated socket's pcb. No per-socket callback unhook is needed —
// all LwIP sockets share the same static event_callback.
this->cached_sock_ = nullptr;
#else
if (this->loop_monitored_) {
App.unregister_socket_fd(this->fd_);
}
#endif
int ret = ::close(this->fd_);
this->fd_ = -1; // Sentinel for "closed" — prevents double-close and makes use-after-close visible.
return ret;
}
int BSDSocketImpl::setblocking(bool blocking) {