Skip to content

feat: route the non-Ruby-thread dispatch path through an optional proxy - #1365

Merged
suketa merged 1 commit into
suketa:mainfrom
otegami:feature/worker-proxy-dispatch
Jun 5, 2026
Merged

feat: route the non-Ruby-thread dispatch path through an optional proxy#1365
suketa merged 1 commit into
suketa:mainfrom
otegami:feature/worker-proxy-dispatch

Conversation

@otegami

@otegami otegami commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

GitHub: GH-1136

This is step 2 of the per-worker proxy work, on top of GH-1364. It wires the
dispatcher so the non-Ruby-thread path (Case 3) can optionally route through a
per-worker proxy instead of the global executor. Behavior is unchanged: the
only in-tree caller passes NULL, so the proxy path is not exercised yet. It
goes live with the scalar integration in the next PR.

For the overall picture I have in mind — the full mechanism with every step
integrated — see #7 on my fork: otegami#7

What it adds

  • rbduckdb_function_executor_dispatch_via_proxy(cb, user_data, proxy): the
    new entry point. When proxy is non-NULL, Case 3 hands the callback to that
    proxy (Case 3a); when NULL, it falls back to the global executor (Case 3b).
    Cases 1 and 2 are unchanged.
  • dispatch_callback_to_proxy: the sender half of the proxy hand-off — sets
    the request under the proxy's lock, signals it, and blocks until done. This
    is the counterpart of the receiver loop merged in Add per-worker proxy thread primitive #1364.
  • rbduckdb_function_executor_dispatch keeps its signature and now delegates
    with NULL, so existing callers are byte-for-byte on the old path.

For reviewer's information

  • The header doc for rbduckdb_worker_proxy_create now states its raise
    contract: the executor runs callbacks unprotected, so a wrapper that
    dispatches create through it must rb_protect the call. The wrapper
    arrives with the first consumer (scalar integration), which is where the
    rb_protect lands.

Summary by CodeRabbit

  • Refactor
    • Improved internal function dispatch mechanism to better handle multi-threaded scenarios and synchronization across different thread types.
    • Enhanced error handling documentation for callback execution to ensure proper exception protection.

Split the dispatcher so the worker-thread path (Case 3) can route through
a per-worker proxy. rbduckdb_function_executor_dispatch_via_proxy is the
new entry point: when proxy is non-NULL it hands the callback to that
proxy (Case 3a), otherwise it falls back to the global executor (Case 3b).
dispatch_callback_to_proxy is the sender half of the proxy hand-off --
the counterpart of the receiver loop that landed with the primitive.

rbduckdb_function_executor_dispatch keeps its signature and now simply
delegates with NULL, so behavior is unchanged -- the only in-tree caller
passes NULL and no proxy is ever exercised yet. Full suite unchanged
(1147). The proxy path goes live once scalar/table integration lands.

Also document rbduckdb_worker_proxy_create's raise contract: the executor
runs callbacks unprotected, so a wrapper that dispatches create through it
must rb_protect the call. The wrapper itself arrives with the first
consumer (scalar integration), which is where the rb_protect lands.
@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 590d9503-46d2-4c4b-a02a-af5625d90f55

📥 Commits

Reviewing files that changed from the base of the PR and between 5b2f10b and 54c8740.

📒 Files selected for processing (2)
  • ext/duckdb/function_executor.c
  • ext/duckdb/function_executor.h

📝 Walkthrough

Walkthrough

This PR extends the DuckDB function executor with a per-worker proxy dispatch path for non-Ruby threads. It introduces a new exported dispatcher that routes callbacks either through a specified per-worker proxy, falls back to a global executor, or executes directly for Ruby threads with/without the GVL.

Changes

Per-worker proxy dispatch extension

Layer / File(s) Summary
Public API contract and proxy requirements
ext/duckdb/function_executor.h
New exported dispatcher declaration rbduckdb_function_executor_dispatch_via_proxy and expanded documentation for rbduckdb_worker_proxy_create that warns callbacks must be wrapped with rb_protect to prevent deadlocks and signaling failures.
Per-worker proxy dispatch implementation
ext/duckdb/function_executor.c
Internal helper dispatch_callback_to_proxy enqueues callback requests onto a worker proxy and blocks until completion using condition variables (MSVC and pthread variants). New dispatcher rbduckdb_function_executor_dispatch_via_proxy chooses between per-worker proxy path, global executor fallback, or direct/GVL-protected execution for Ruby threads.

Sequence Diagram

(Omitted—changes introduce a dispatch routing mechanism without a multi-component interaction flow requiring visualization.)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • suketa/ruby-duckdb#1257: Refactors the global executor-thread dispatch into the shared function executor module that this PR extends with per-worker proxy routing.
  • suketa/ruby-duckdb#1364: Introduces the worker_proxy lifecycle primitives (rbduckdb_worker_proxy_create/destroy) that this PR routes callbacks through.
  • suketa/ruby-duckdb#1280: Updates table_function.c to call the refactored rbduckdb_function_executor_dispatch for GVL-safe callback execution.

Suggested reviewers

  • suketa

Poem

🐰 Through proxy threads and conditions so fine,
Non-Ruby paths now beautifully align,
Dispatchers route with synchronized care,
GVL-safe callbacks everywhere!
A worker proxy dance, both swift and fair. 🎭

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 clearly and concisely describes the main change: adding optional proxy routing for non-Ruby-thread dispatch, which is the primary objective of this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
ext/duckdb/function_executor.c

In file included from ext/duckdb/function_executor.c:1:
ext/duckdb/ruby-duckdb.h:7:10: fatal error: 'ruby.h' file not found
7 | #include "ruby.h"
| ^~~~~~~~
1 error generated.
Error: the following clang command did not run successfully:
/opt/infer-linux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/bin/clang-18
@/tmp/coderabbit-infer/54c8740a66d671ca018392b7823b562921b39569-33dc795747e490ed/tmp/clang_command_.tmp.c470c6.txt
++Contents of '/tmp/coderabbit-infer/54c8740a66d671ca018392b7823b562921b39569-33dc795747e490ed/tmp/clang_command_.tmp.c470c6.txt':
"-cc1" "-load"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../../facebook-clang-plugins/libtooling/build/FacebookClangPlugin.dylib"
"-add-plugin" "BiniouASTExporter" "-plugin-arg-BiniouASTExporter" "-"
"-plugin-arg-BiniouASTExporter" "PREPEND_CURRENT_DIR=1"
"-plugin-arg-BiniouASTExporter" "MAX_STRING_SIZE=65535" "-cc1" "-triple"
"x86_64-unknown-linux-gnu" "-emit-

... [truncated 752 characters] ...

inux-x86_64-v1.2.0/lib/infer/facebook-clang-plugins/clang/install/lib/clang/18/include"
"-internal-isystem" "/usr/local/include" "-internal-isystem"
"/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
"-internal-externc-isystem" "/usr/include/x86_64-linux-gnu"
"-internal-externc-isystem" "/include" "-internal-externc-isystem"
"/usr/include" "-Wno-ignored-optimization-argument" "-Wno-everything"
"-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf"
"-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o"
"/tmp/coderabbit-infer/33dc795747e490ed/file.o" "-x" "c"
"ext/duckdb/function_executor.c" "-O0" "-fno-builtin" "-include"
"/opt/infer-linux-x86_64-v1.2.0/lib/infer/infer/bin/../lib/clang_wrappers/global_defines.h"
"-Wno-everything"


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.

@suketa suketa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LGTM, thank you.

@suketa
suketa merged commit 739ec0c into suketa:main Jun 5, 2026
35 checks passed
@otegami
otegami deleted the feature/worker-proxy-dispatch branch June 6, 2026 10:53
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.

2 participants