@samitouri / QOSAMI-WSL / commits / 09326c4f

Avoid std::terminate in FsUserContext destructor (Plan9) (#40417)

* Replace throwing macros with logging in FsUserContext destructor FsUserContext::~FsUserContext() used THROW_LAST_ERROR_IF() which throws exceptions. If this destructor runs during stack unwinding from another exception, std::terminate is called immediately. Replace with LOG_LAST_ERROR_IF() to log failures without throwing. These syscalls (setresuid/setresgid/setgroups to restore root) should virtually never fail, but if they do, logging is the appropriate response in a destructor. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Use try/CATCH_LOG since LOG_LAST_ERROR_IF is not defined on Linux Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Ben Hillis <benhillis@microsoft.com>

Ben Hillis committed May 11, 2026 at 22:05 UTC 09326c4f55c7b681d5cf4073de7c172db70fd6bf
1 file changed +12 -8
src/linux/plan9/p9util.cpp
+12 -8
@@ -242,17 +242,21 @@ FsUserContext::FsUserContext(uid_t uid, gid_t gid, const std::vector<gid_t>& gro
242 // Restores the effective uid and gid to root.
243 FsUserContext::~FsUserContext()
244 {
245 - if (m_Restore)
245 + try
246 {
247 - // Use the syscall directly since the wrappers change the value on all threads.
248 - THROW_LAST_ERROR_IF(sys_setresuid(-1, 0, -1) < 0);
249 - THROW_LAST_ERROR_IF(sys_setresgid(c_InvalidGid, 0, c_InvalidGid) < 0);
250 - }
247 + if (m_Restore)
248 + {
249 + // Use the syscall directly since the wrappers change the value on all threads.
250 + THROW_LAST_ERROR_IF(sys_setresuid(-1, 0, -1) < 0);
251 + THROW_LAST_ERROR_IF(sys_setresgid(c_InvalidGid, 0, c_InvalidGid) < 0);
252 + }
253
252 - if (m_restoreGroups)
253 - {
254 - THROW_LAST_ERROR_IF(sys_setgroups(0, nullptr) < 0);
254 + if (m_restoreGroups)
255 + {
256 + THROW_LAST_ERROR_IF(sys_setgroups(0, nullptr) < 0);
257 + }
258 }
259 + CATCH_LOG()
260 }
261
262 } // namespace p9fs::util