diff --git a/Kernel/Devices/TTY/MasterPTY.cpp b/Kernel/Devices/TTY/MasterPTY.cpp index 7c262b6c7d..a6a571fea5 100644 --- a/Kernel/Devices/TTY/MasterPTY.cpp +++ b/Kernel/Devices/TTY/MasterPTY.cpp @@ -36,10 +36,14 @@ MasterPTY::MasterPTY(unsigned index, NonnullOwnPtr buffer) , m_buffer(move(buffer)) { m_buffer->set_unblock_callback([this]() { - m_slave.with([this](auto& slave) { - if (slave) - evaluate_block_conditions(); - }); + // Note that has_slave() takes and then releases the m_slave spinlock. + // Not holding the spinlock while calling evaluate_block_conditions is legal, + // as the call will trigger a check to see if waiters may be unblocked, + // and if it was called spuriously (i.e. because the slave disappeared between + // calling the unblock callback and the actual block condition evaluation), + // the waiters will simply not unblock. + if (has_slave()) + evaluate_block_conditions(); }); } diff --git a/Kernel/Devices/TTY/MasterPTY.h b/Kernel/Devices/TTY/MasterPTY.h index 9d10d49c70..803af8abd6 100644 --- a/Kernel/Devices/TTY/MasterPTY.h +++ b/Kernel/Devices/TTY/MasterPTY.h @@ -43,6 +43,11 @@ private: virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg) override; virtual StringView class_name() const override { return "MasterPTY"sv; } + bool has_slave() const + { + return m_slave.with([](auto& slave) -> bool { return slave; }); + } + SpinlockProtected, LockRank::None> m_slave; unsigned m_index; bool m_closed { false };