Skip to content

fix: Zemu.checkAndPullImage should reliably reject#565

Open
turbocrime wants to merge 2 commits intoZondax:mainfrom
turbocrime:check-and-pull-image-failure
Open

fix: Zemu.checkAndPullImage should reliably reject#565
turbocrime wants to merge 2 commits intoZondax:mainfrom
turbocrime:check-and-pull-image-failure

Conversation

@turbocrime
Copy link
Copy Markdown

@turbocrime turbocrime commented Jul 30, 2025

if the network request made by checkAndPullImage fails for any reason, sometimes the promise stalls forever.

this PR attempts to make promise rejection more reliable.

relevant to #564

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling during image pulling to ensure errors are properly reported and prevent potential runtime issues.
  • Tests

    • Added new tests to verify correct error handling when pulling invalid images.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 30, 2025

Walkthrough

The EmuContainer.checkAndPullImage method in src/emulator.ts was refactored to improve error handling by using Promise rejection instead of direct error throwing and by adding checks for stream validity. Additionally, a new test suite was introduced in tests/check-and-pull-image.test.ts to verify error handling in both EmuContainer and Zemu when image pulling fails.

Changes

Cohort / File(s) Change Summary
Error Handling Refactor in EmuContainer
src/emulator.ts
Refactored checkAndPullImage to use Promise rejection for errors, updated callback parameter types, and added a check for stream validity before proceeding.
Unit Tests for Image Pull Failure
tests/check-and-pull-image.test.ts
Added tests to verify that checkAndPullImage in both EmuContainer and Zemu correctly handle and propagate errors when image pulling fails.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Knowledge Base: Disabled due to Reviews > Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 1ba263d and 4eb1f03.

📒 Files selected for processing (2)
  • src/emulator.ts (2 hunks)
  • tests/check-and-pull-image.test.ts (1 hunks)
🔇 Additional comments (8)
src/emulator.ts (5)

73-73: LGTM! Proper Promise constructor usage.

The Promise constructor now correctly uses both resolve and reject parameters, which is essential for proper error handling and addresses the core issue where promises could stall indefinitely.


74-74: LGTM! Proper callback parameter handling.

The callback signature correctly handles both error and stream parameters from the docker.pull operation, supporting the improved error handling flow.


82-87: LGTM! Critical fix for promise rejection in callback.

Changing from throw err to reject(err) in the onFinished callback is essential. Previously, throwing an error in this nested callback wouldn't properly reject the outer Promise, causing it to hang indefinitely. Now errors are correctly propagated as Promise rejections.


89-92: LGTM! Consistent error handling for immediate failures.

Using reject(err) instead of throw err for immediate docker.pull errors maintains consistency with the callback error handling and ensures the Promise is properly rejected rather than throwing synchronously.


94-96: LGTM! Good defensive programming for stream validation.

Adding a check for stream existence before calling docker.modem.followProgress prevents potential runtime errors. This is a solid defensive programming practice.

Consider: Should there be an else clause to handle the case where stream is falsy? Currently, if stream is null/undefined, the promise would resolve without any progress tracking, which might be the intended behavior, but it's worth confirming.

tests/check-and-pull-image.test.ts (3)

1-8: LGTM! Well-structured test setup.

The imports, test suite structure, and constant definition for the invalid image name are well-organized and follow good testing practices.


9-11: LGTM! Direct test of error handling.

This test directly verifies that EmuContainer.checkAndPullImage properly rejects when given an invalid image name, which validates the core promise rejection fix implemented in the source code.


13-22: LGTM! Comprehensive test of error propagation through delegation.

This test effectively verifies that:

  1. Zemu.checkAndPullImage properly delegates to EmuContainer.checkAndPullImage
  2. The expected DEFAULT_EMU_IMG parameter is passed
  3. Error propagation works correctly through the delegation layer

The mock implementation is well-designed, intercepting the call to verify the parameter and then forwarding to trigger the error condition.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

import { DEFAULT_EMU_IMG } from '../src/constants'

describe('checkAndPullImage fails', () => {
const BAD_IMAGE_NAME = 'this is not a valid image name'
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i encountered the issue due to network errors, but it seems like simply requesting an invalid image name can activate the same conditions.

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.

1 participant