Skip to content

Proper handling thread creation failure #3063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

josesimoes
Copy link
Member

@josesimoes josesimoes commented Jan 9, 2025

Description

  • Now freeing allocated memory.
  • Throwing general exception, not the code for the EE to process exception.

Motivation and Context

  • On thread creation failure there would be a memory leak.
  • Returning the code to process exception is not the way to go. Lacking a specific one, should be using a general exception instead.

How Has This Been Tested?

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for thread creation in multiple device communication modules
    • Enhanced resource management during thread initialization failures
    • Refined error reporting mechanisms to provide clearer failure indications

- Now freeing allocated memory.
- Throwing general exception, not the code for the EE to process exception.
Copy link

coderabbitai bot commented Jan 9, 2025

Walkthrough

The pull request introduces consistent error handling improvements across multiple AzureRTOS platform-specific implementations for I2C and OneWire device interfaces. The changes focus on refining thread creation error handling by ensuring proper memory cleanup and using a more generalized error code (CLR_E_FAIL) when thread creation fails. These modifications aim to enhance resource management and error reporting in scenarios where thread initialization encounters issues.

Changes

File Change Summary
targets/AzureRTOS/ST/_nanoCLR/System.Device.I2c/sys_dev_i2c_native_System_Device_I2c_I2cDevice.cpp Updated error handling in NativeTransmit method to free thread stack memory and use CLR_E_FAIL on thread creation failure
targets/AzureRTOS/SiliconLabs/_nanoCLR/System.Device.I2c/sys_dev_i2c_native_System_Device_I2c_I2cDevice.cpp Similar error handling modification as ST implementation, replacing CLR_E_PROCESS_EXCEPTION with CLR_E_FAIL
targets/AzureRTOS/SiliconLabs/_nanoCLR/nanoFramework.Device.OneWire/nf_dev_onewire_nanoFramework_Device_OneWire_OneWireHost.cpp Enhanced error handling in FindOneDevice function with memory cleanup and generalized error reporting

Possibly related PRs

Suggested labels

Type: enhancement, Area: Common libs, Platform: STM32, Platform: NXP, Platform: TI-SimpleLink

Finishing Touches

  • 📝 Generate Docstrings

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
targets/AzureRTOS/SiliconLabs/_nanoCLR/nanoFramework.Device.OneWire/nf_dev_onewire_nanoFramework_Device_OneWire_OneWireHost.cpp (1)

474-478: LGTM! Proper cleanup on thread creation failure.

The changes correctly handle thread creation failure by freeing the allocated stack memory before returning the error code, preventing potential memory leaks.

However, consider adding a debug log message to help with troubleshooting thread creation failures in development builds.

 if (status != TX_SUCCESS)
 {
     // failed to create the thread
+#if !defined(BUILD_RTM)
+    // Log the actual error code in debug builds
+    DEBUG_PRINT("Failed to create 1-Wire thread. Status: %d\n", status);
+#endif
     // free stack memory
     platform_free(workingThreadStack);

     NANOCLR_SET_AND_LEAVE(CLR_E_FAIL);
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f86e48f and 654a134.

📒 Files selected for processing (3)
  • targets/AzureRTOS/ST/_nanoCLR/System.Device.I2c/sys_dev_i2c_native_System_Device_I2c_I2cDevice.cpp (1 hunks)
  • targets/AzureRTOS/SiliconLabs/_nanoCLR/System.Device.I2c/sys_dev_i2c_native_System_Device_I2c_I2cDevice.cpp (1 hunks)
  • targets/AzureRTOS/SiliconLabs/_nanoCLR/nanoFramework.Device.OneWire/nf_dev_onewire_nanoFramework_Device_OneWire_OneWireHost.cpp (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: nf-interpreter (Check_Code_Style)
  • GitHub Check: nf-interpreter (Nightly build) (Check_Build_Options)
  • GitHub Check: nf-interpreter (Check_Build_Options)
🔇 Additional comments (4)
targets/AzureRTOS/SiliconLabs/_nanoCLR/System.Device.I2c/sys_dev_i2c_native_System_Device_I2c_I2cDevice.cpp (1)

586-590: LGTM! Improved error handling for thread creation failure.

The changes properly handle resource cleanup by freeing allocated memory before returning the error code when thread creation fails.

targets/AzureRTOS/ST/_nanoCLR/System.Device.I2c/sys_dev_i2c_native_System_Device_I2c_I2cDevice.cpp (1)

583-587: LGTM! Consistent error handling improvement.

The changes align with the SiliconLabs implementation, ensuring proper resource cleanup on thread creation failure.

targets/AzureRTOS/SiliconLabs/_nanoCLR/nanoFramework.Device.OneWire/nf_dev_onewire_nanoFramework_Device_OneWire_OneWireHost.cpp (2)

Line range hint 8-9: Verify thread stack size consistency.

The thread stack size should be consistent with similar operations across the codebase.

✅ Verification successful

Thread stack size is appropriate

The ONEWIRE_THREAD_STACK_SIZE of 256 bytes is consistent with similar peripheral operations in the codebase, particularly matching the I2C operations thread stack size.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check thread stack size definitions across the codebase
# Look for thread stack size definitions to ensure consistency

# Search for thread stack size definitions
rg 'THREAD_STACK_SIZE.*[0-9]+'

# Search for similar OneWire operations thread definitions
rg 'OneWire.*STACK_SIZE'

Length of output: 6431


474-478: Verify consistent thread creation error handling across the codebase.

Let's ensure that other thread creation instances follow the same pattern of proper cleanup on failure.

✅ Verification successful

Thread creation error handling is consistent across the codebase

The reviewed code follows the established pattern used throughout the codebase for thread creation error handling:

  1. Proper memory cleanup on thread creation failure
  2. Consistent error codes (CLR_E_FAIL for thread creation, CLR_E_OUT_OF_MEMORY for allocation)
  3. Resource cleanup in correct order (thread deletion followed by stack memory freeing)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find similar thread creation patterns to verify consistent error handling
# Look for tx_thread_create calls and check their error handling

# Search for thread creation patterns
ast-grep --pattern 'tx_thread_create($$$)'

# Search for potential memory leaks in thread creation error handling
rg -A 5 'tx_thread_create.*TX_SUCCESS'

Length of output: 7735


Script:

#!/bin/bash
# Get more context around thread creations to see error handling patterns
# Looking at both before and after thread creation

# OneWire implementation
rg -B 10 -A 10 "tx_thread_create.*WaitingTask" targets/AzureRTOS/SiliconLabs/_nanoCLR/nanoFramework.Device.OneWire/

# I2C implementations
rg -B 10 -A 10 "tx_thread_create.*WorkingThread" targets/AzureRTOS/ST/_nanoCLR/System.Device.I2c/
rg -B 10 -A 10 "tx_thread_create.*WorkingThread" targets/AzureRTOS/SiliconLabs/_nanoCLR/System.Device.I2c/

# WiFi implementation
rg -B 10 -A 10 "tx_thread_create.*wifiConnectThread" targets/AzureRTOS/ST/_common/drivers/wifi/

Length of output: 424


Script:

#!/bin/bash
# Search for thread stack allocation and error handling patterns

# Look for stack allocations and surrounding context
rg -B 5 -A 15 "ThreadStack.*=.*platform_malloc" 

# Look for thread creation error handling patterns
rg -B 5 -A 10 "if.*status.*!=.*TX_SUCCESS" 

# Get content of specific files we know have thread creation
cat "targets/AzureRTOS/SiliconLabs/_nanoCLR/nanoFramework.Device.OneWire/nf_dev_onewire_nanoFramework_Device_OneWire_OneWireHost.cpp" | grep -A 10 -B 10 "workingThreadStack"
cat "targets/AzureRTOS/ST/_nanoCLR/System.Device.I2c/sys_dev_i2c_native_System_Device_I2c_I2cDevice.cpp" | grep -A 10 -B 10 "WorkingThreadStack"

Length of output: 59683

@josesimoes josesimoes merged commit 3fd2ac0 into nanoframework:main Jan 9, 2025
8 checks passed
@josesimoes josesimoes deleted the fix-thread-creation-handling branch January 9, 2025 15:17
josesimoes added a commit that referenced this pull request Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants