Skip to content

Conversation

@renecannao
Copy link
Contributor

@renecannao renecannao commented Feb 9, 2026

Summary

Fixes a crash that occurred immediately when running any PostgreSQL query on macOS and FreeBSD systems.

Problem

ProxySQL crashed with a NULL pointer dereference in CopyCmdMatcher::match() when processing PostgreSQL queries on macOS and FreeBSD.

Backtrace:

* thread #11, stop reason = EXC_BAD_ACCESS (code=1, address=0x5c)
  frame #5: CopyCmdMatcher::match(this=0x0000000000000000, query="SELECT ?;", ...) const at PgSQL_Thread.h:149:10
  frame #6: PgSQL_Session::handler(...) at PgSQL_Session.cpp:2924:27

Root Cause

copy_cmd_matcher was guarded by #ifdef IDLE_THREADS, but IDLE_THREADS is explicitly disabled on macOS (__APPLE__) and FreeBSD in proxy_defines.h:5-8:

#if !defined(__FreeBSD__) && !defined(__APPLE__)
#define IDLE_THREADS
#endif

This created an inconsistent configuration where:

  • PgSQL_Thread::copy_cmd_matcher was not defined on macOS/FreeBSD
  • PgSQL_Session::copy_cmd_matcher was always defined
  • The initialization from thread to session was compiled out
  • Usage at PgSQL_Session.cpp:2924 was not guarded → NULL pointer dereference

Solution

Removed #ifdef IDLE_THREADS guards from copy_cmd_matcher since:

  1. COPY command detection is a fundamental PostgreSQL feature - it detects COPY ... FROM STDIN/STDOUT commands for fast-forward mode
  2. It has no dependency on idle threads - the feature works independently of the threading model
  3. This is the correct fix - disabling COPY support on macOS/FreeBSD would be incorrect

Changes

  • include/PgSQL_Thread.h - Moved CopyCmdMatcher *copy_cmd_matcher; outside #ifdef IDLE_THREADS
  • lib/PgSQL_Thread.cpp - Removed #ifdef IDLE_THREADS guards from initialization/cleanup
  • lib/Base_Thread.cpp - Removed #ifdef IDLE_THREADS guard from copy operation

Testing

Tested manually on macOS - PostgreSQL queries now work correctly without crashes.

Affected Systems

  • macOS (__APPLE__)
  • FreeBSD (__FreeBSD__)

Fixes #5359

Summary by CodeRabbit

  • Refactor
    • Internal code reorganization for improved maintainability. Conditional compilation directives were restructured to simplify the codebase without affecting user-facing functionality.

Fix NULL pointer dereference in CopyCmdMatcher that caused ProxySQL
to crash immediately when running any PostgreSQL query on macOS and
FreeBSD systems.

Root cause: copy_cmd_matcher was guarded by #ifdef IDLE_THREADS, but
IDLE_THREADS is explicitly disabled on macOS (__APPLE__) and FreeBSD
in proxy_defines.h. This caused the pointer to be NULL when accessed.

The fix removes #ifdef IDLE_THREADS guards from copy_cmd_matcher since
COPY command detection is a fundamental PostgreSQL feature for fast-
forward mode and has no dependency on idle threads.

Affected systems: macOS, FreeBSD (where IDLE_THREADS is disabled)

Fixes: #5359
@gemini-code-assist
Copy link

Summary of Changes

Hello @renecannao, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a significant stability issue where PostgreSQL queries would cause a crash on macOS and FreeBSD. The problem stemmed from an incorrect conditional compilation directive that prevented the proper initialization of a key component, copy_cmd_matcher, leading to a NULL pointer dereference. By removing these platform-specific compilation guards, the CopyCmdMatcher is now consistently available and correctly initialized, restoring full PostgreSQL functionality on the affected operating systems.

Highlights

  • Crash Fix: Addresses a critical crash occurring on macOS and FreeBSD systems when executing PostgreSQL queries, caused by a NULL pointer dereference.
  • Root Cause Identified: The crash was traced to copy_cmd_matcher being conditionally compiled under #ifdef IDLE_THREADS, which is explicitly disabled on macOS and FreeBSD. This led to an uninitialized pointer being accessed in PgSQL_Session::handler.
  • Conditional Compilation Removal: Removed the #ifdef IDLE_THREADS guards around copy_cmd_matcher declarations and operations, ensuring it is always compiled and properly initialized across all platforms.
  • PostgreSQL COPY Command Support: The fix ensures that the essential PostgreSQL COPY command detection, which is independent of idle threads, functions correctly on macOS and FreeBSD.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • include/PgSQL_Thread.h
    • Moved the declaration of CopyCmdMatcher *copy_cmd_matcher; outside of the #ifdef IDLE_THREADS block to ensure it's always included.
  • lib/Base_Thread.cpp
    • Removed the #ifdef IDLE_THREADS guard surrounding the assignment of _sess->copy_cmd_matcher, making the assignment unconditional.
  • lib/PgSQL_Thread.cpp
    • Removed #ifdef IDLE_THREADS guards from the destructor, init() method, and constructor related to copy_cmd_matcher initialization and cleanup, ensuring these operations are always performed.
Activity
  • No review comments or additional activity have been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link

coderabbitai bot commented Feb 9, 2026

📝 Walkthrough

Walkthrough

The pull request removes #ifdef IDLE_THREADS conditional compilation guards around the CopyCmdMatcher member across three files. The CopyCmdMatcher pointer is relocated from a conditional block to unconditional declaration in the header, and its initialization and cleanup logic are unconditionally compiled. This ensures consistent compilation behavior across all platforms.

Changes

Cohort / File(s) Summary
Header Declaration
include/PgSQL_Thread.h
Relocated CopyCmdMatcher *copy_cmd_matcher declaration from inside #ifdef IDLE_THREADS conditional block to public member section, ensuring it is always declared regardless of platform or build configuration.
Initialization & Cleanup
lib/PgSQL_Thread.cpp
Removed #ifdef IDLE_THREADS guards around CopyCmdMatcher instantiation in init() method and deletion in destructor, making allocation and deallocation unconditional.
Session Registration
lib/Base_Thread.cpp
Removed #ifdef IDLE_THREADS guards around the assignment _sess->copy_cmd_matcher in register_session() function, making the copy operation unconditional.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 The copy matcher hops so free,
No longer bound by #ifdef decree,
On every platform it shall thrive,
Keeping PostgreSQL connections alive! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: resolving a crash on macOS/FreeBSD when running PostgreSQL queries due to NULL pointer dereference in CopyCmdMatcher.
Linked Issues check ✅ Passed All coding requirements from issue #5359 are met: copy_cmd_matcher declaration moved outside #ifdef IDLE_THREADS guards [#5359], initialization/cleanup guards removed [#5359], and register_session() copy guard removed [#5359].
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the NULL pointer dereference issue; no unrelated modifications or feature additions are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch v3.0-5359

No actionable comments were generated in the recent review. 🎉


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a crash on macOS and FreeBSD systems when executing PostgreSQL queries. The root cause was a NULL pointer dereference related to copy_cmd_matcher, which was incorrectly defined within an #ifdef IDLE_THREADS block that is disabled on these platforms. The fix involves moving the copy_cmd_matcher declaration and its lifecycle management (initialization, assignment, and cleanup) outside of the conditional compilation block. The changes are correct, well-reasoned, and effectively resolve the bug. The code is clean and the fix is well-contained.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 9, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on macOS: NULL pointer dereference in CopyCmdMatcher when running any PostgreSQL query

1 participant