Skip to content

Fix "Undefined array key 0" warning in MultiRPC constructor#36

Merged
roxblnfk merged 2 commits into
roadrunner-php:4.xfrom
SpartakusMd:fix/multirpc-undefined-array-key-0
May 11, 2026
Merged

Fix "Undefined array key 0" warning in MultiRPC constructor#36
roxblnfk merged 2 commits into
roadrunner-php:4.xfrom
SpartakusMd:fix/multirpc-undefined-array-key-0

Conversation

@SpartakusMd
Copy link
Copy Markdown
Contributor

@SpartakusMd SpartakusMd commented May 11, 2026

self::$freeRelays is not guaranteed to be 0-indexed: entries are removed via unset(self::$freeRelays[$relayIndex]) in occupyRelay() and occupyRelayAsync() (lines 171, 194), and PHP's auto-increment retains the highest key ever used when appending. After enough churn in a long-running worker (e.g. RoadRunner), the static array can end up with keys like [1 => ..., 3 => ...]count() > 0 holds, but self::$freeRelays[0] raises Warning: Undefined array key 0.

Switch to array_key_first(), mirroring the $occupiedRelays branch two lines below.

Summary by CodeRabbit

  • Bug Fixes
    • Improved relay selection mechanism to handle non-standard array indexing, ensuring proper relay assignment in all scenarios.

Review Change Stack

`self::$freeRelays` is not guaranteed to be 0-indexed: entries are
removed via `unset(self::$freeRelays[$relayIndex])` in `occupyRelay()`
and `occupyRelayAsync()` (lines 171, 194), and PHP's auto-increment
retains the highest key ever used when appending. After enough churn
in a long-running worker (e.g. RoadRunner), the static array can end
up with keys like `[1 => ..., 3 => ...]` — `count() > 0` holds, but
`self::$freeRelays[0]` raises `Warning: Undefined array key 0`.

Switch to `array_key_first()`, mirroring the `$occupiedRelays` branch
two lines below.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6800cfbc-f5fb-415c-ba19-252fd889cb96

📥 Commits

Reviewing files that changed from the base of the PR and between 2a37211 and c269c83.

📒 Files selected for processing (1)
  • src/RPC/MultiRPC.php

📝 Walkthrough

Walkthrough

The PR updates MultiRPC::__construct() to use array_key_first() when selecting an existing relay from self::$freeRelays, replacing a hard-coded index access that assumed zero-indexed contiguous keys.

Changes

Relay Selection Robustness

Layer / File(s) Summary
Free Relay Array Key Selection
src/RPC/MultiRPC.php
Constructor now calls array_key_first(self::$freeRelays) instead of assuming index 0, enabling correct relay selection with non-zero or non-contiguous array keys.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐰 A clever fix, so small and neat,
Array keys no longer cause defeat,
array_key_first() leads the way,
Through rabbit holes both night and day! 🔑✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 specific fix: replacing direct index access with array_key_first() to resolve the 'Undefined array key 0' warning in the MultiRPC constructor.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

@roxblnfk roxblnfk merged commit a14454c into roadrunner-php:4.x May 11, 2026
12 checks passed
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