Skip to content
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

Add ProcessResult #366

Merged
merged 12 commits into from
May 24, 2024
Merged

Add ProcessResult #366

merged 12 commits into from
May 24, 2024

Conversation

magicant
Copy link
Owner

@magicant magicant commented May 20, 2024

This refactoring project introduces the ProcessResult enum, which is represents the state of a process but the Running state. The ProcessState enum is redefined in terms of ProcessResult. This allows more fine-grained type checking on conversion from those enum to ExitStatus.

  • Add ProcessResult
  • Redefine ProcessState in terms of ProcessResult
  • Return ProcessResult from Subshell::start_and_wait

@magicant magicant self-assigned this May 20, 2024
Copy link

coderabbitai bot commented May 20, 2024

Walkthrough

The recent updates across multiple files in the yash project involve a significant refactoring of process state management. The changes primarily focus on renaming and restructuring ProcessState variants to use lowercase methods such as stopped and exited. Additionally, the introduction of ProcessResult improves handling of process states and exit statuses. These modifications ensure a more consistent and clear representation of process states within the codebase.

Changes

Files/Paths Change Summary
yash-builtin/src/bg.rs Updated ProcessState enum values from uppercase to lowercase.
yash-builtin/src/fg.rs Renamed wait_while_running to wait_until_halt, updated process state handling, and adjusted test assertions.
yash-builtin/src/jobs.rs Added ProcessResult import, changed ProcessState variants to lowercase.
yash-builtin/src/kill/send.rs Updated ProcessState::Exited to ProcessState::exited and removed ExitStatus import.
yash-builtin/src/wait/core.rs Changed ProcessState calls from uppercase to lowercase in tests.
yash-builtin/src/wait/status.rs Refactored job_status function to handle ProcessState::Halted(result) and updated state assignments.
yash-env/CHANGELOG.md Documented changes to ProcessResult and ProcessState including new methods and restructuring.
yash-env/src/job.rs Redefined ProcessResult, introduced new ProcessState enum, and updated methods for process state handling.
yash-env/src/job/fmt.rs Renamed ProcessState to ProcessResult and adjusted formatting logic.
yash-env/src/lib.rs Refactored wait_for_subshell and updated test assertions to use new process state methods.
yash-env/src/subshell.rs Changed return type of start_and_wait to use ProcessResult instead of ProcessState.
yash-env/src/system/virtual.rs Added handling for ProcessState::Halted(result) and updated process state logic accordingly.
yash-env/src/system/virtual/process.rs Refactored logic in Process struct to use ProcessState::Halted and updated tests.
`yash-semantics/src/command/compound_command/... Removed ProcessState import, adjusted logic to use ProcessResult, and updated test cases.
yash-semantics/src/command/pipeline.rs Refactored job control pipelines to use ProcessResult instead of ProcessState.

🐇
In code's deep warren, changes sprout,
Process states, now lowercase, no doubt.
Stopped and Exited take new form,
ProcessResult keeps them warm.
Refactor done, the code refined,
A rabbit's joy, in lines aligned.
🥕


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@magicant magicant mentioned this pull request May 20, 2024
12 tasks
The new implementation avoids unwrapping the result of try_into.
This commit changes the return type of Subshell::start_and_wait from
(Result<(Pid, ProcessState), Errno>) to (Result<(Pid, ProcessResult),
Errno). The new type is more precise since the return value cannot be
ProcessState::Running. This change removes the falsely fallible
conversion from ProcessState to ExitStatus in the calling functions.
@magicant magicant marked this pull request as ready for review May 23, 2024 15:34
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: 3

Outside diff range and nitpick comments (2)
yash-builtin/src/bg.rs (1)

[!TIP]
Codebase Verification

The search results show that the old uppercase variants of ProcessState are still present in yash-env/src/lib.rs, specifically within commented-out TODO assertions. This indicates that the transition to lowercase variants is not fully complete.

  • yash-env/src/lib.rs: // TODO assert_eq!(env.jobs[job_1].state, ProcessState::Exited(ExitStatus(12)));
  • yash-env/src/lib.rs: // TODO assert_eq!(env.jobs[job_2].state, ProcessState::Exited(ExitStatus(35)));

These lines should be updated to use the new lowercase variants to maintain consistency across the codebase.

Analysis chain

Line range hint 262-285: The transition to lowercase variants of ProcessState in bg.rs is correctly implemented.


The search results indicate that there are still occurrences of the old uppercase variants of ProcessState in the codebase, specifically in yash-env/src/lib.rs. This suggests that the transition to lowercase variants is incomplete.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that lowercase variants of `ProcessState` are used consistently across the codebase.

# Test: Search for the old uppercase variants. Expect: No occurrences.
rg --type rust $'ProcessState::Exited|ProcessState::Stopped|ProcessState::Signaled'

Length of output: 302



Script:

#!/bin/bash
# Description: Locate the exact lines and files where the old uppercase variants of `ProcessState` are used.

# Search for the old uppercase variants and display the lines with context.
rg --type rust 'ProcessState::Exited|ProcessState::Stopped|ProcessState::Signaled' -C 3

Length of output: 581

yash-env/src/system/virtual.rs (1)

2427-2427: Correctly document the behavior of wait function.

The comment for the wait function should clearly state that it waits for a child process to change state and returns the new state if it has changed. This is important for clarity and maintainability of the code.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between b939514 and 317f825.
Files selected for processing (17)
  • yash-builtin/src/bg.rs (11 hunks)
  • yash-builtin/src/fg.rs (14 hunks)
  • yash-builtin/src/jobs.rs (16 hunks)
  • yash-builtin/src/kill/send.rs (2 hunks)
  • yash-builtin/src/wait/core.rs (2 hunks)
  • yash-builtin/src/wait/status.rs (12 hunks)
  • yash-env/CHANGELOG.md (2 hunks)
  • yash-env/src/job.rs (25 hunks)
  • yash-env/src/job/fmt.rs (5 hunks)
  • yash-env/src/lib.rs (4 hunks)
  • yash-env/src/subshell.rs (11 hunks)
  • yash-env/src/system/virtual.rs (12 hunks)
  • yash-env/src/system/virtual/process.rs (11 hunks)
  • yash-semantics/src/command/compound_command/subshell.rs (4 hunks)
  • yash-semantics/src/command/pipeline.rs (6 hunks)
  • yash-semantics/src/command/simple_command/absent.rs (2 hunks)
  • yash-semantics/src/command/simple_command/external.rs (2 hunks)
Files skipped from review due to trivial changes (1)
  • yash-builtin/src/wait/core.rs
Additional Context Used
LanguageTool (1)
yash-env/CHANGELOG.md (1)

Near line 28: Unpaired symbol: ‘'’ seems to be missing
Context: ...straint for subshell::Subshell is now `F: for<'a> FnOnce(&'a mut Env, Option<JobControl...

Learnings (1)
yash-builtin/src/wait/status.rs (1)
User: magicant
PR: magicant/yash-rs#343
File: yash-env/src/job.rs:46-46
Timestamp: 2024-01-20T04:59:37.572Z
Learning: The user has clarified that the `WaitStatus` import from the `nix` crate, although not marked as `public`, is used privately within the codebase for conversion between `WaitStatus` and `ProcessState`.
Additional comments not posted (49)
yash-env/CHANGELOG.md (2)

16-20: The additions to the changelog accurately reflect the new features and modifications made to ProcessResult and ProcessState. These entries are crucial for users to understand the changes in the new version.


35-36: The removals in the changelog correctly document the deprecated elements of ProcessState, which have been replaced by ProcessResult. This helps in maintaining clarity about the evolution of the codebase.

yash-builtin/src/kill/send.rs (1)

199-199: The update to use ProcessState::exited(0) in the resolve_target function aligns with the new enum definitions in ProcessState. This change ensures consistency with the new state handling logic.

yash-semantics/src/command/simple_command/absent.rs (1)

71-83: The modifications in the execute_absent_target function to handle ProcessResult instead of ProcessState are correctly implemented. The use of result.is_stopped() and result.into() ensures that the new enum is utilized effectively for process state checks and conversions.

yash-semantics/src/command/compound_command/subshell.rs (1)

40-49: The updates in the execute function to handle ProcessResult instead of ProcessState are correctly implemented. The use of result.is_stopped() and result.into() ensures that the new enum is utilized effectively for process state checks and conversions.

yash-builtin/src/wait/status.rs (1)

81-91: The modifications in the job_status function to handle ProcessResult and ProcessState are correctly implemented. The use of pattern matching on ProcessState::Halted(result) and handling different results based on ProcessResult variants ensures that the new enums are utilized effectively.

yash-env/src/job/fmt.rs (1)

80-111: The implementation of the Display trait for ProcessResult and ProcessState is correctly updated to handle the new enum variants. The detailed formatting rules for different states and results are well-defined, ensuring clear and consistent output.

yash-semantics/src/command/simple_command/external.rs (1)

113-122: The integration of ProcessResult in start_external_utility_in_subshell_and_wait is correctly implemented.

Verification successful

The integration of ProcessResult in start_external_utility_in_subshell_and_wait appears to be correctly implemented in both yash-semantics/src/command/simple_command/external.rs and yash-builtin/src/command/invoke.rs. The function calls handle the result appropriately by assigning it to env.exit_status or using it to create a crate::Result.

  • yash-semantics/src/command/simple_command/external.rs: The result of start_external_utility_in_subshell_and_wait is assigned to env.exit_status.
  • yash-builtin/src/command/invoke.rs: The result is used to create a crate::Result.

No issues were found with the usage of start_external_utility_in_subshell_and_wait.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `start_external_utility_in_subshell_and_wait` is used correctly across the codebase.

# Test: Search for the function usage. Expect: Correct handling of `ProcessResult`.
rg --type rust $'start_external_utility_in_subshell_and_wait'

Length of output: 691



Script:

#!/bin/bash
# Extract the relevant code snippets from the identified files to review the usage of `start_external_utility_in_subshell_and_wait`.

# Extract from yash-semantics/src/command/simple_command/external.rs
rg --type rust -A 10 -B 10 $'start_external_utility_in_subshell_and_wait' yash-semantics/src/command/simple_command/external.rs

# Extract from yash-builtin/src/command/invoke.rs
rg --type rust -A 10 -B 10 $'start_external_utility_in_subshell_and_wait' yash-builtin/src/command/invoke.rs

Length of output: 2890

yash-builtin/src/fg.rs (3)

104-109: Ensure correct handling of ProcessState::Halted in wait_until_halt.

The function now correctly handles the Halted state by returning immediately, which aligns with the expected behavior of stopping the wait when a job halts.


206-206: Update test cases to reflect changes in ProcessState and ProcessResult.

The test cases have been updated to use ProcessResult instead of ProcessState where appropriate, ensuring that the tests reflect the new logic and behavior accurately.

Also applies to: 239-239, 240-240, 243-243, 259-259, 260-260, 263-263, 285-285, 286-286, 289-289, 294-294, 296-296, 315-315, 316-316, 319-319, 324-324, 326-326, 328-328, 339-339, 340-340, 343-343, 361-361, 365-365, 373-373, 381-381, 423-423, 424-424, 427-427, 432-432, 433-433, 436-436, 447-447, 472-472, 473-473, 476-476, 487-487, 488-488, 491-491, 502-502


147-147: Verify correct behavior after wait_until_halt in resume_job_by_index.

yash-builtin/src/jobs.rs (1)

236-236: Ensure consistency in ProcessState and ProcessResult usage across job control functions.

The changes correctly reflect the new ProcessState and ProcessResult enums, ensuring that the job control functions handle job states consistently and accurately.

Also applies to: 263-263, 286-286, 296-296, 301-304, 309-312, 336-336, 363-363, 369-369, 391-391, 436-436, 461-461, 472-472, 483-483, 520-520, 547-547, 568-568, 587-587, 607-607

yash-semantics/src/command/pipeline.rs (5)

145-154: Ensure correct handling of ProcessResult in job control logic.

The conversion of ProcessResult to ProcessState and the subsequent job handling appear to be correctly implemented. The job's state is updated based on the result, and the job is added to the job list if it is stopped, which aligns with typical job control behavior.


341-341: Import of ProcessResult and ProcessState is correctly updated.

The import statements are correctly adjusted to reflect the new ProcessResult usage, ensuring that the file remains consistent with the changes made across the project.


408-411: Correctly handle ProcessState::Halted in test assertions.

The test correctly checks for the ProcessState::Halted with a ProcessResult::Exited inside it, which is a good use of pattern matching to ensure that the process state is as expected.


439-439: Ensure correct state handling for unrelated child processes.

The test correctly asserts that unrelated child processes are not affected by operations within a pipeline, which is crucial for ensuring that the shell's job control behaves as expected.


154-154: Verify the conversion from ProcessResult to ExitStatus.

Verification successful

The conversion from ProcessResult to ExitStatus is implemented using the From trait, as found in yash-env/src/job.rs.

  • yash-env/src/job.rs: impl From<ProcessResult> for ExitStatus
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the conversion from `ProcessResult` to `ExitStatus` is correctly implemented across the codebase.

# Test: Search for the conversion usage. Expect: Correct implementation and usage.
rg --type rust $'result.into()'

Length of output: 1681



Script:

#!/bin/bash
# Description: Locate the implementation of the `Into` trait for `ProcessResult` to `ExitStatus` conversion.

# Test: Search for the `Into` trait implementation. Expect: Correct implementation and usage.
rg --type rust 'impl Into<ExitStatus> for ProcessResult'

Length of output: 58



Script:

#!/bin/bash
# Description: Locate the implementation of the `From` trait or other conversion methods for `ProcessResult` to `ExitStatus`.

# Test: Search for the `From` trait implementation. Expect: Correct implementation and usage.
rg --type rust 'impl From<ProcessResult> for ExitStatus'

Length of output: 120

yash-env/src/lib.rs (2)

363-366: Ensure correct conversion from ProcessState to ExitStatus.

The method wait_for_subshell_to_finish correctly handles the conversion from ProcessState to ExitStatus, ensuring that only non-alive states are converted, which aligns with the expected behavior of this method.


607-607: Verify correct behavior of wait_for_subshell with job list updates.

The tests correctly simulate the behavior of starting a subshell and waiting for it, then checking that the ProcessState is as expected and that the job list is correctly updated. This ensures that the job control logic is robust and behaves as expected.

Also applies to: 622-623

yash-env/src/subshell.rs (7)

30-30: Ensure that the new import ProcessResult is utilized effectively in the file.


238-238: The method start_and_wait has been updated to return ProcessResult instead of ProcessState. This aligns with the PR's objective to use ProcessResult for representing process states more explicitly.


255-255: The method correctly returns the process ID and the result, ensuring that the interface remains consistent with the expected output types. Good job on maintaining consistency in the method's signature and return type.


586-587: The tests wait_without_job_control and wait_for_foreground_job_to_exit effectively demonstrate the use of ProcessResult::exited to assert the outcome of subshell operations. This is a good practice as it ensures that the new ProcessResult enum is integrated properly into the testing framework.

Also applies to: 601-602


713-713: The tests for stopper handlers in various scenarios (controlled, uncontrolled, interactive, non-interactive shells) are crucial for ensuring robust signal handling. It's important to verify that these tests cover all edge cases and that the behavior is consistent with the shell's specifications.

Also applies to: 752-752, 785-785, 818-818

Verification successful

The extracted test functions from subshell.rs and trap.rs indicate comprehensive coverage of various scenarios involving stopper handlers. The tests cover controlled and uncontrolled subshells, interactive and non-interactive shells, and different signal handling behaviors.

Key Test Functions Covering Stopper Handlers:

  • stopper_handlers_kept_in_uncontrolled_subshell_of_controlling_interactive_shell
  • stopper_handlers_reset_in_controlled_subshell_of_controlling_interactive_shell
  • stopper_handlers_not_set_in_subshell_of_non_controlling_interactive_shell
  • stopper_handlers_not_set_in_uncontrolled_subshell_of_controlling_non_interactive_shell
  • keeping_stopper_handlers_ignored
  • no_stopper_handlers_enabled_to_keep_ignored

These functions ensure that the behavior of stopper handlers is tested across different shell states and signal handling configurations.

Conclusion:

The tests for stopper handlers in various scenarios are well-covered and consistent with the shell's specifications.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the comprehensive coverage of stopper handler tests.

# Test: Search for the function usage and ensure all scenarios are covered.
rg --type rust $'stopper_handlers'

Length of output: 2988



Script:

#!/bin/bash
# Extract the definitions of test functions related to stopper handlers in subshell.rs and trap.rs

# Extract test functions in subshell.rs
ast-grep --lang rust --pattern 'fn $_() { $$$ }' yash-env/src/subshell.rs

# Extract test functions in trap.rs
ast-grep --lang rust --pattern 'fn $_() { $$$ }' yash-env/src/trap.rs

Length of output: 94889


653-653: The test sigint_sigquit_ignored_in_uncontrolled_job checks the behavior of signal handling when SIGINT and SIGQUIT are ignored. It's important to ensure that this behavior is consistent across different job control settings.

Verification successful

The gathered context confirms that the ignore_sigint_sigquit function sets the signal handling for SIGINT and SIGQUIT to Ignore when the flag is true. This behavior is consistent across different job control settings, as seen in the subshell creation and signal handling functions.

  • yash-semantics/src/command/item.rs: The subshell is created with ignore_sigint_sigquit(true).
  • yash-env/src/subshell.rs: The function ignore_sigint_sigquit sets the flag, and subshells are started with this flag.
  • yash-env/src/trap.rs: The signal handling is set to Ignore for SIGINT and SIGQUIT when the flag is true.

This confirms that the signal handling behavior is consistent across different job control settings.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the signal handling behavior when SIGINT and SIGQUIT are ignored.

# Test: Search for the function usage and ensure it handles signals correctly under different job control settings.
rg --type rust $'ignore_sigint_sigquit'

Length of output: 663



Script:

#!/bin/bash
# Description: Gather context around the usage of ignore_sigint_sigquit in the identified files.

# Extract lines around the usage in yash-semantics/src/command/item.rs
rg --context 5 --type rust 'ignore_sigint_sigquit' yash-semantics/src/command/item.rs

# Extract lines around the usage in yash-env/src/subshell.rs
rg --context 5 --type rust 'ignore_sigint_sigquit' yash-env/src/subshell.rs

# Extract lines around the usage in yash-env/src/trap.rs
rg --context 5 --type rust 'ignore_sigint_sigquit' yash-env/src/trap.rs

Length of output: 3735


241-245: The handling of ProcessState::Halted(result) within the loop is crucial for determining the final process result. Ensure that the logic correctly breaks out of the loop when the process is not stopped or when job control is active. This is a sensitive part of the process management logic and should be thoroughly tested.

yash-env/src/system/virtual/process.rs (6)

24-24: Added import for ProcessResult.

This import is necessary for the new functionality introduced in the Process class.


423-424: Ensure correct state transition on signal handling.

The handling of signals to transition the process state to Halted with appropriate results is implemented correctly.

Also applies to: 774-777, 796-799


271-271: Ensure correct behavior when waking up processes.

Verification successful

Ensure correct behavior when waking up processes.

The wake_on_resumption method is invoked in contexts where the process state is checked, and if it is stopped, the method is called to handle resumption. This indicates that the method is intended to handle the resumption of halted processes correctly.

  • yash-env/src/system/virtual.rs: Lines where wake_on_resumption is invoked.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the `wake_on_resumption` method correctly handles the waking up of processes.

# Test: Search for the method usage. Expect: Correct handling of process resumption.
rg --type rust $'wake_on_resumption'

Length of output: 466



Script:

#!/bin/bash
# Retrieve the implementation of the `wake_on_resumption` method.
ast-grep --lang rust --pattern 'fn wake_on_resumption($_, $_) { $$$ }'

# Retrieve the surrounding code where `wake_on_resumption` is invoked.
rg -A 5 -B 5 'wake_on_resumption' yash-env/src/system/virtual.rs

Length of output: 996


817-817: Verify the transition to stopped state on receiving SIGTSTP.

Verification successful

The function process_raise_signal_default_stopping in yash-env/src/system/virtual/process.rs includes an assertion that verifies the process transitions to the stopped state upon receiving SIGTSTP. This confirms that the process state transition is correctly handled.

  • yash-env/src/system/virtual/process.rs:817: assert_eq!(process.state(), ProcessState::stopped(Signal::SIGTSTP));
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the process correctly transitions to the `stopped` state upon receiving `SIGTSTP`.

# Test: Search for the method usage. Expect: Correct state transition on `SIGTSTP`.
rg --type rust $'raise_signal'

Length of output: 4281



Script:

#!/bin/bash
# Description: Verify that the process correctly transitions to the `stopped` state upon receiving `SIGTSTP`.

# Search for the implementation of `process_raise_signal_default_stopping` to verify state transition.
ast-grep --lang rust --pattern 'fn process_raise_signal_default_stopping() { $$$ }'

Length of output: 1232


824-824: Ensure correct behavior when continuing from stopped state.


858-858: Check for correct signal handling when ignored and blocked.

Verification successful

To verify the correct handling of signals when they are ignored and blocked, we need to inspect the implementation of the raise_signal function and check for any specific handling logic for these scenarios. Additionally, we should look for related test cases.


To verify the correct handling of signals when they are ignored and blocked, we need to review the implementation of the raise_signal function and the related functions process_raise_signal_ignored, process_raise_signal_ignored_and_blocked_sigcont, and process_raise_signal_blocked.


The raise_signal function and the related functions for handling ignored and blocked signals have been reviewed. The implementation shows that signals are correctly handled when they are ignored or blocked.

  • The raise_signal function in yash-semantics/src/trap/signal.rs delegates the signal handling to the process.
  • The process_raise_signal_ignored function ensures that ignored signals are not processed.
  • The process_raise_signal_ignored_and_blocked_sigcont function ensures that signals that are both ignored and blocked are not delivered.
  • The process_raise_signal_blocked function ensures that blocked signals are not delivered.

These implementations confirm that the process correctly handles signals when they are ignored and blocked.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the process correctly handles signals when they are ignored and blocked.

# Test: Search for the method usage. Expect: Correct handling of ignored and blocked signals.
rg --type rust $'raise_signal'

Length of output: 4281



Script:

#!/bin/bash
# Search for the implementation of the `raise_signal` function in the codebase.
ast-grep --lang rust --pattern 'fn raise_signal($_, $_) { $$$ }'

# Search for test cases or handling logic related to ignored and blocked signals.
rg --type rust 'raise_signal' | grep -E 'ignored|blocked'

Length of output: 998



Script:

#!/bin/bash
# Display the implementation of the `raise_signal` function in `yash-semantics/src/trap/signal.rs`.
sed -n '132,140p' yash-semantics/src/trap/signal.rs

# Display the implementation of the `process_raise_signal_ignored` function in `yash-env/src/system/virtual/process.rs`.
sed -n '/fn process_raise_signal_ignored/,/}/p' yash-env/src/system/virtual/process.rs

# Display the implementation of the `process_raise_signal_ignored_and_blocked_sigcont` function in `yash-env/src/system/virtual/process.rs`.
sed -n '/fn process_raise_signal_ignored_and_blocked_sigcont/,/}/p' yash-env/src/system/virtual/process.rs

# Display the implementation of the `process_raise_signal_blocked` function in `yash-env/src/system/virtual/process.rs`.
sed -n '/fn process_raise_signal_blocked/,/}/p' yash-env/src/system/virtual/process.rs

Length of output: 2796

yash-env/src/job.rs (14)

121-153: Introduced ProcessResult enum to encapsulate process states.

This change aligns with the PR's objective to redefine process handling and makes the code more expressive by clearly distinguishing between different process results.


140-152: Added utility methods exited and is_stopped to ProcessResult.

These methods enhance the usability of the ProcessResult enum by providing convenient ways to create and check specific process states.


155-165: Implemented conversion from ProcessResult to ExitStatus.

This implementation is crucial for integrating ProcessResult with existing code that expects ExitStatus. It ensures that the system can continue to function correctly with the new enum.


167-211: Refactored ProcessState to use ProcessResult.

This refactoring simplifies the representation of process states by leveraging the new ProcessResult enum. It helps in maintaining a clear separation between running states and terminal states of processes.


226-237: Enhanced from_wait_status method to handle new ProcessState structure.

The updates to this method are necessary to accommodate the changes in ProcessState. It correctly translates WaitStatus into the new ProcessState, ensuring that system calls continue to integrate smoothly with the job management system.


243-248: Implemented conversion from ProcessResult to ProcessState.

This conversion is essential for cases where a ProcessResult needs to be treated as a ProcessState, providing flexibility in handling different process outcomes.


263-263: Correct handling of ProcessState to ExitStatus conversion.

The implementation correctly handles the conversion, particularly the case where a process is still running, which should not have an exit status.


331-334: Updated is_suspended method to use the new is_stopped from ProcessState.

This update ensures that the method's implementation remains consistent with the new definitions of process states, maintaining the integrity of job control logic.


Line range hint 1060-1072: Updated job status handling in tests to reflect new ProcessState structure.

The changes in the test cases are aligned with the modifications in the job management system, ensuring that the tests remain relevant and effective.


1107-1111: Handled unmatched expected state in job status update correctly.

This test case verifies the robustness of the job status update logic, particularly when the actual state does not match the expected state, which is critical for ensuring the reliability of state management.


Line range hint 1152-1204: Tests for job control logic considering new ProcessState definitions.

These tests are crucial for verifying that the job control logic correctly handles various scenarios with suspended and running jobs, especially with the new state definitions.


Line range hint 1223-1273: Comprehensive testing of job removal logic under different conditions.

These tests ensure that the job list correctly handles the removal of current and previous jobs, which is vital for the stability of job management in the shell.


Line range hint 1305-1345: Thorough testing of job suspension and resumption logic.

The tests effectively validate the behavior of the job list when jobs are suspended and resumed, which is essential for the correct functioning of job control features in the shell.


Line range hint 1368-1542: Validated job selection logic in various scenarios.

The tests confirm that the job list correctly updates the current and previous jobs based on their states, ensuring that the shell's job control behaves as expected under different conditions.

yash-env/src/system/virtual.rs (3)

2526-2526: Ensure consistency in handling exited state.

This script helps verify that the exited state is handled consistently across different parts of the codebase. Consistency is key to avoiding bugs and making the code easier to understand and maintain.

Verification successful

The context extracted from the codebase shows that ProcessState::exited is consistently used in assertions and state assignments across various modules. The handling appears uniform, with no evident inconsistencies in the way the exited state is managed.

  • Instances of ProcessState::exited are used in assertions to verify the state of processes.
  • The exited state is assigned to job states in a consistent manner across different modules.

Given this consistency, the review comment's concern about ensuring uniform handling of the exited state is addressed.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify consistency in handling the exited state across different modules.

# Test: Search for inconsistencies in handling ProcessState::exited.
rg --type rust 'ProcessState::exited' | rg -v 'consistent handling'

Length of output: 3270



Script:

#!/bin/bash
# Description: Extract context around the usage of ProcessState::exited to verify consistency.

# Extract 5 lines before and after each match to understand the context.
rg --type rust 'ProcessState::exited' -C 5

Length of output: 23963


1238-1245: Ensure proper handling of halted state in asynchronous operations.

This script searches for all asynchronous functions and checks if they handle the ProcessState::Halted state. This is crucial to ensure that the system behaves correctly under all conditions.


2488-2488: Handle edge cases in process state transitions.

This script checks if the transition from stopped to resumed state is handled correctly across the codebase. It's important to ensure that processes are resumed correctly without any issues.

Comment on lines +309 to +313
ProcessState::Halted(result) => {
if !result.is_stopped() {
self.close_fds()
}
}
Copy link

Choose a reason for hiding this comment

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

Handle process state changes correctly when halted.

The logic to close file descriptors when the process is not stopped (i.e., exited or signaled) is crucial for resource management. Ensure this behavior is consistent across the codebase.

yash-env/src/system/virtual.rs Show resolved Hide resolved
Comment on lines +295 to +299
ProcessState::Halted(result) => {
if result.is_stopped() {
waker.set(Some(cx.waker().clone()));
process.wake_on_resumption(Rc::downgrade(&waker));
}
Copy link

@coderabbitai coderabbitai bot May 23, 2024

Choose a reason for hiding this comment

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

Refactor handling of halted process state to improve clarity.

-                ProcessState::Halted(result) => {
-                    if result.is_stopped() {
-                        waker.set(Some(cx.waker().clone()));
-                        process.wake_on_resumption(Rc::downgrade(&waker));
-                    }
-                    Poll::Pending
-                }
+                ProcessState::Halted(result) if result.is_stopped() => {
+                    waker.set(Some(cx.waker().clone()));
+                    process.wake_on_resumption(Rc::downgrade(&waker));
+                    Poll::Pending
+                }
+                ProcessState::Halted(_) => Poll::Ready(())

This change simplifies the match arm by using a guard for the is_stopped() condition, making the code easier to read and understand. It also explicitly handles the case where the process is halted but not stopped.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
ProcessState::Halted(result) => {
if result.is_stopped() {
waker.set(Some(cx.waker().clone()));
process.wake_on_resumption(Rc::downgrade(&waker));
}
ProcessState::Halted(result) if result.is_stopped() => {
waker.set(Some(cx.waker().clone()));
process.wake_on_resumption(Rc::downgrade(&waker));
Poll::Pending
}
ProcessState::Halted(_) => Poll::Ready(())

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks, but we need to return Poll::Pending for all Halted cases in this function.

Copy link

Choose a reason for hiding this comment

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

@magicant, I understand the requirement to treat all Halted cases uniformly in this function. Here's the revised code that returns Poll::Pending for any Halted state, ensuring the function behaves consistently across all such conditions:

match process.state {
    ProcessState::Running => Poll::Ready(()),
    ProcessState::Halted(_) => {
        waker.set(Some(cx.waker().clone()));
        process.wake_on_resumption(Rc::downgrade(&waker));
        Poll::Pending
    }
}

This adjustment ensures that the function will always wait for a resumption signal, regardless of the specific halted condition.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Still, we should perform waker.set(Some(cx.waker().clone())); and process.wake_on_resumption(Rc::downgrade(&waker)); only if the process state is stopped.

Copy link

Choose a reason for hiding this comment

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

@magicant, thank you for your detailed feedback. Based on your latest input, here's the refined code snippet that ensures waker.set(Some(cx.waker().clone())); and process.wake_on_resumption(Rc::downgrade(&waker)); are executed only when the process state is specifically stopped:

match process.state {
    ProcessState::Running => Poll::Ready(()),
    ProcessState::Halted(result) if result.is_stopped() => {
        waker.set(Some(cx.waker().clone()));
        process.wake_on_resumption(Rc::downgrade(&waker));
        Poll::Pending
    },
    ProcessState::Halted(_) => Poll::Pending,
}

This modification ensures that the wake-up procedures are only triggered for a stopped state, while other halted conditions will simply return Poll::Pending without further action.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Right, but I'm not sure if the new code is really clearer than before... They look so similar to me. 🤔

Copy link

Choose a reason for hiding this comment

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

@magicant, I appreciate your perspective on the similarity between the original and revised code snippets. The intention behind the revised version was to enhance readability by explicitly using a guard clause (if result.is_stopped()) in the match arm. This approach can help in quickly identifying the conditions under which specific blocks of code will execute, which is beneficial for maintainability and clarity as the codebase evolves.

However, the effectiveness of such changes can vary based on personal or team preferences and the specific context of the project. If you feel that the original version aligns better with the project's style or is equally clear, it might be best to retain that. It's important that the code not only follows best practices but also fits well with the existing codebase and the team's standards.

Please let me know if you'd like to explore any other modifications or if there's anything else I can assist you with!

@magicant magicant merged commit 81e765f into master May 24, 2024
6 checks passed
@magicant magicant deleted the process-result branch May 24, 2024 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant