Skip to content

Throw when FixedPortGenerator runs out of ports - #1383

Closed
cristianrgreco wants to merge 2 commits into
mainfrom
claude/fixed-port-generator-bounds
Closed

Throw when FixedPortGenerator runs out of ports#1383
cristianrgreco wants to merge 2 commits into
mainfrom
claude/fixed-port-generator-bounds

Conversation

@cristianrgreco

Copy link
Copy Markdown
Collaborator

Summary

FixedPortGenerator.generatePort() returned this.ports[this.portIndex++] with no bounds check, yielding undefined (typed as number) once the pool was exhausted. It now throws a clear error so misuse fails fast.

Verification

  • Added a unit test asserting the first call returns the seeded port and the next throws (no Docker required).
  • npm ci && npx vitest run packages/testcontainers/src/utils/port-generator.test.tspassed
  • npm run check-compiles → clean · npm run lint → clean

Test results

New unit test passing.

Not breaking

Adds a thrown error only on the previously-undefined exhaustion path (which returned a non-number). No change to valid usage or to RandomPortGenerator.

🤖 Generated with Claude Code

FixedPortGenerator.generatePort() returned this.ports[index++] with no
bounds check, yielding undefined (typed as number) once the pool was
exhausted. Throw a clear error instead so misuse fails fast.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@cristianrgreco cristianrgreco added bug Something isn't working patch Backward compatible bug fix labels Jun 22, 2026
@netlify

netlify Bot commented Jun 22, 2026

Copy link
Copy Markdown

Deploy Preview for testcontainers-node ready!

Name Link
🔨 Latest commit 3f8f3d8
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-node/deploys/6a3a3c2854f69b0008a71abd
😎 Deploy Preview https://deploy-preview-1383--testcontainers-node.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c01e7b578

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


public generatePort(): Promise<number> {
if (this.portIndex >= this.ports.length) {
throw new Error("FixedPortGenerator has no more ports available");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the promise contract on exhaustion

generatePort() is typed through PortGenerator as returning Promise<number>, but this branch throws before returning a Promise. In contexts that consume the promised API directly, such as generator.generatePort().catch(...) or collecting calls for Promise.all, exhaustion will escape synchronously instead of being handled as a rejection; RandomPortGenerator and the interface both expose asynchronous failure semantics, so this should reject the returned promise (for example by making the method async or returning Promise.reject(...)).

Useful? React with 👍 / 👎.

generatePort() is typed as Promise<number> via the PortGenerator
interface, so exhaustion should surface as a promise rejection rather
than a synchronous throw. Make the method async so callers using
.catch() or Promise.all observe consistent async failure semantics.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@cristianrgreco

Copy link
Copy Markdown
Collaborator Author

Closing: FixedPortGenerator is an internal, test-only helper — it is not exported from the package and is not referenced by any production code (only RandomPortGenerator is). The exhaustion path this guarded can't be reached today, so the change isn't worth carrying.

@cristianrgreco
cristianrgreco deleted the claude/fixed-port-generator-bounds branch June 23, 2026 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working patch Backward compatible bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant