From 7e7d6cfbcd81d9e32ee1eaa1eea59d11eadc7342 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 28 Oct 2026 17:25:03 -0400 Subject: [PATCH] crypto: skip clearing empty OpenSSL error queues ClearErrorOnReturn calls ERR_clear_error() when it is created and again when it goes out of scope. That function resets all 16 slots of the thread's error queue, including two free() calls per slot, even when the queue is empty, which is almost always the case. Check for a queued error first, which costs a fraction of that. ClearErrorOnReturn guards most crypto operations, including every CryptoJob, so this applies to all of them. Signed-off-by: Yagiz Nizipli Assisted-by: Grok PR-URL: https://github.com/nodejs/node/pull/67109 Reviewed-By: Filip Skokan --- deps/ncrypto/ncrypto.cc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/deps/ncrypto/ncrypto.cc b/deps/ncrypto/ncrypto.cc index 28a1431467c..ef811c0bc81 100644 --- a/deps/ncrypto/ncrypto.cc +++ b/deps/ncrypto/ncrypto.cc @@ -340,14 +340,22 @@ constexpr BoringSSLCipher kBoringSSLCiphers[] = { // ============================================================================ +namespace { +// ERR_clear_error() resets every slot of the error queue even when it is +// empty, which costs several times more than checking for an error first. +void ClearErrorQueue() { + if (ERR_peek_error() != 0) ERR_clear_error(); +} +} // namespace + ClearErrorOnReturn::ClearErrorOnReturn(CryptoErrorList* errors) : errors_(errors) { - ERR_clear_error(); + ClearErrorQueue(); } ClearErrorOnReturn::~ClearErrorOnReturn() { if (errors_ != nullptr) errors_->capture(); - ERR_clear_error(); + ClearErrorQueue(); } unsigned long ClearErrorOnReturn::peekError() { // NOLINT(runtime/int)