Skip to content

Error comparisons match on error category name, so system- and generic-category codes never compare equal #18946

Open

Description

@kevinushey

Summary

core::Error::operator==(const boost::system::error_code&) (and the Error == Error overload) compares the numeric code together with the error category name (src/cpp/shared_core/Error.cpp). Boost's own error_code == errc comparison uses semantic equivalence across categories; ours does not. An Error therefore only matches a code that was built in the same category it was built from, and the codebase has two ways of building an errc-valued Error that land in different categories:

  • systemError(boost::system::errc::X, location) -> the int overload -> boost::system::system_category() (name "system").
  • Error(boost::system::errc::X, location) -> implicit errc -> error_code conversion -> boost::system::generic_category() (name "generic").

Every existing classification check compares against the system-category form (e.g. error == systemError(boost::system::errc::no_such_file_or_directory, ErrorLocation()), FileLock::isNoLockAvailable, http::isConnectionUnavailableError, the source database supervisor's isLockingUnsupported), so an Error built with the generic form is invisible to all of them.

This bit for real in #18941 / #18944: startHttpConnectionListenerWithTimeout compared the listener's bind error against error_code(errc::address_in_use, generic_category()), while asio reports bind failures in the system category, so the "retry on address in use" loop it guarded had never run. That instance is fixed in #18944; this issue is for what is left.

Latent instances

  1. Generic-category constructions. src/cpp/core/StringUtils.cpp:687 and roughly twenty sites in src/cpp/core/Database.cpp build errors with Error(boost::system::errc::X, ...) (invalid_argument, not_connected, operation_not_supported, resource_unavailable_try_again). Nothing compares against those codes today, so there is no observed misbehavior, but any future error == systemError(errc::X, ...) check against them fails silently -- exactly the failure mode duplicate session launch locks the user out: the aborting duplicate removes the live session's socket #18941 had.

  2. AsyncConnectionImpl::handleStreamError (src/cpp/core/include/core/http/AsyncConnectionImpl.hpp) converts an Error back into error_code(error.getCode(), generic_category()) -- the comment there already says the category is wrong. FixedBufferProxy::handleErrorImpl re-wraps that code into an Error and runs it through isConnectionTerminatedError, whose comparisons are against asio-category codes, so that classification can never match. Today StreamWriter::handleError filters the connection-terminated cases out before the conversion, so the visible effect is limited to a stream-write failure being logged a second time under the wrong category name. It is one refactor away from misclassifying client disconnects as errors.

  3. (Related, Windows-only, lower priority.) Several sites compare against errno-valued codes on all platforms -- src/cpp/core/r_util/RToolsInfo.cpp:403, src/cpp/core/http/Util.cpp:701, and the getCode() checks in src/cpp/session/modules/SessionChat.cpp -- while the producers report Win32 codes there. ERROR_FILE_NOT_FOUND happens to equal ENOENT (2), so file-not-found matches by coincidence; ERROR_PATH_NOT_FOUND (3) and ERROR_ACCESS_DENIED (5, vs EACCES 13) never match. The effect is a less specific message or an extra logged error. shared_core/FilePath.cpp's isFileNotFoundError / isPathNotFoundError / isNotFoundError already handle this correctly and are the pattern to copy.

Possible fixes

  • Make Error::operator==(const boost::system::error_code&) (and ideally Error == Error) use boost's equivalence rules instead of comparing category names, e.g. by resolving the stored category name back to a category for the categories we use (system, generic, asio.misc, asio.ssl, the RStudio custom categories) and comparing through error_condition. That makes category drift stop mattering everywhere at once, and lets handleStreamError hand a correctly categorized code back to its handler.
  • Or, more narrowly: remove the Error(boost::system::errc::X, ...) spelling in favor of systemError(...), add a boost::system::error_code toErrorCode(const Error&) helper for handleStreamError, and add a unit test in shared_core that pins the expected equality semantics so the next drift is caught.

Either way, add a test that Error(errc::X, loc) == systemError(errc::X, loc), which is the property everything in the codebase implicitly assumes.

Activity

  1. added theissue type on Sep 25, 2026
  2. kevinushey commented on Sep 26, 2026

    @kevinushey
    CollaboratorAuthor

    Update: #18944 no longer changes the startHttpConnectionListenerWithTimeout comparison, so that instance is open again and belongs here. Server sessions no longer reach that loop for a socket in use (they exit at the early claim), but TCP sessions (desktop, and standalone/launcher sessions reusing a port) still do.

    When fixing it: each retry calls initializeHttpConnectionListener(), which allocates a new listener without freeing the previous one, so once the comparison matches, every retry leaks a listener along with its io_context and socket. Free or reuse the listener in the same change.

  3. self-assigned this
    on Nov 20, 2026
  4. kevinushey commented on Nov 20, 2026

    @kevinushey
    CollaboratorAuthor

    #19753 covers part of this:

    • Group 1: systemError(errc::X) now builds a generic-category error, the same as Error(errc::X), and ErrorTests pins that the two compare equal.
    • Group 3: the remaining getCode() / value() == errc::X checks, including the ones in SessionChat.cpp, now use the new Error::matches(errc::X), or Boost's ec == errc::X for a raw error_code. The RToolsInfo.cpp and http/Util.cpp sites had already been fixed.

    Still open here:

    • Group 2: AsyncConnectionImpl::handleStreamError() still rebuilds the stream write's error as error_code(error.getCode(), generic_category()).
    • The busy-port retry: don't let a duplicate session launch take over a live session's socket #18944 fixed the comparison in SessionMain.cpp, but startHttpConnectionListenerWithTimeout() in SessionHttpMethods.cpp still compares the bind error against error_code(errc::address_in_use, generic_category()), so its retry still never runs. Enabling it (e.g. with error.matches(errc::address_in_use)) needed two follow-on fixes in fix errno values read as system error codes #19749: dispose of the listener that failed to bind before creating the next one, and log the TCP listener's startup warnings once rather than on every attempt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions