Skip to content

Conversation

assembler-0
Copy link
Owner

@assembler-0 assembler-0 commented Jul 12, 2025

Summary by CodeRabbit

  • New Features

    • Introduced deferred process termination, allowing processes to be safely cleaned up after termination.
    • Added new process states to better track processes awaiting cleanup.
    • Enhanced process metadata to record termination reasons, exit codes, and parent process IDs.
    • Added new functions to terminate processes by ID and to retrieve system tick count.
  • Improvements

    • Improved scheduler handling of processes in the process of termination or awaiting cleanup.
    • Unified and strengthened process cleanup and termination handling across the system.

Copy link

coderabbitai bot commented Jul 12, 2025

Walkthrough

The changes introduce a deferred process termination system by adding new process states and a termination queue. Scheduler and process management logic are updated to handle dying and zombie processes, defer cleanup, and unify termination handling. New metadata fields and enums are added to track termination reasons and process hierarchy.

Changes

File(s) Change Summary
Kernel/Core/Kernel.c Removed a commented line before the assembly instruction enabling interrupts; no functional change.
Kernel/Process/Process.c Added deferred process termination with a queue, new TerminateProcess function, updated scheduler and cleanup logic.
Kernel/Process/Process.h Added PROC_ZOMBIE and PROC_DYING states, TerminationReason enum, new fields to Process struct, and new API declarations.

Sequence Diagram(s)

sequenceDiagram
    participant Process
    participant Scheduler
    participant TerminationQueue
    participant Cleanup

    Process->>Scheduler: Calls TerminateProcess(pid, reason, code)
    Scheduler->>TerminationQueue: Enqueue process as DYING
    Scheduler->>Process: Mark as DYING, remove from scheduling
    Note right of Scheduler: Process state transitions to ZOMBIE

    loop On schedule
        Scheduler->>TerminationQueue: Check for ZOMBIE/TERMINATED processes
        TerminationQueue->>Cleanup: Dequeue and clean up process
        Cleanup->>Process: Free resources, reset struct, free slot
    end
Loading

Possibly related PRs

  • Development #23: Both PRs modify FastSchedule and process lifecycle handling, introducing or altering process states and scheduling logic.
  • Development #14: Both PRs enhance process termination and cleanup, with changes to process exit stubs and resource management in Kernel/Process/Process.c.

Poem

A process once ran, then it started to fade,
Into DYING and ZOMBIE, its state was remade.
With queues for the cleanup, and reasons to track,
The scheduler’s smarter—no process comes back!
So here’s to new endings, deferred with great care,
The kernel’s a rabbit, with cycles to spare!
🐰✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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 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: 1

🧹 Nitpick comments (2)
Kernel/Process/Process.h (1)

63-66: Good additions for process lifecycle tracking.

The termination metadata fields are well-chosen. Note that parent_pid appears to be unused in the current implementation - consider documenting its intended future use or removing it if not needed.

Kernel/Process/Process.c (1)

54-93: Well-implemented deferred termination mechanism.

The function properly handles all termination scenarios including the currently running process. Line 76 has an outdated comment that can be removed since GetSystemTicks() is already implemented.

-    proc->termination_time = GetSystemTicks(); // You'll need to implement this
+    proc->termination_time = GetSystemTicks();
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c45d257 and 5d1da02.

📒 Files selected for processing (3)
  • Kernel/Core/Kernel.c (0 hunks)
  • Kernel/Process/Process.c (6 hunks)
  • Kernel/Process/Process.h (3 hunks)
💤 Files with no reviewable changes (1)
  • Kernel/Core/Kernel.c
🔇 Additional comments (8)
Kernel/Process/Process.h (3)

31-33: LGTM! Well-structured process state additions.

The new PROC_ZOMBIE and PROC_DYING states properly support deferred cleanup and prevent race conditions during termination.


36-42: Well-designed termination reason enumeration.

The enum provides good coverage of termination scenarios and will help with debugging and logging process lifecycle events.


116-117: Function declarations match implementation.

The new functions are properly declared and align with the deferred termination design.

Kernel/Process/Process.c (5)

355-391: Excellent handling of dying processes in the scheduler.

The scheduler properly skips context saving for terminated processes and ensures they're not rescheduled. This prevents potential crashes from accessing invalid process state.


407-415: Good defensive programming with state verification.

Verifying the process state before scheduling prevents race conditions where a process might have been terminated between dequeuing and scheduling.


545-559: Properly integrated with the new termination system.

Using TerminateProcess ensures consistent cleanup and the infinite halt loop provides a good safeguard.


644-688: Excellent batched cleanup implementation.

The limited cleanup per call (3 processes) prevents interrupt latency issues while ensuring eventual cleanup of all terminated processes. The deferred cleanup via termination queue is much more efficient than scanning all processes.


732-754: Security subsystem properly integrated with termination system.

The state checks prevent unnecessary validation of terminated processes, and using TerminateProcess ensures proper cleanup of security violations.

Comment on lines +36 to +42
static void AddToTerminationQueue(uint32_t slot) {
if (term_queue_count >= MAX_PROCESSES) return;

termination_queue[term_queue_tail] = slot;
term_queue_tail = (term_queue_tail + 1) % MAX_PROCESSES;
term_queue_count++;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Potential issue: Silent failure when termination queue is full.

The function silently returns when the queue is full, which could lead to zombie processes that never get cleaned up.

Consider logging an error or panic when the queue is full:

 static void AddToTerminationQueue(uint32_t slot) {
-    if (term_queue_count >= MAX_PROCESSES) return;
+    if (term_queue_count >= MAX_PROCESSES) {
+        PrintKernelError("[KERNEL] Termination queue full! Cannot add slot ");
+        PrintKernelInt(slot);
+        PrintKernelError("\n");
+        Panic("Termination queue overflow");
+    }
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static void AddToTerminationQueue(uint32_t slot) {
if (term_queue_count >= MAX_PROCESSES) return;
termination_queue[term_queue_tail] = slot;
term_queue_tail = (term_queue_tail + 1) % MAX_PROCESSES;
term_queue_count++;
}
static void AddToTerminationQueue(uint32_t slot) {
if (term_queue_count >= MAX_PROCESSES) {
PrintKernelError("[KERNEL] Termination queue full! Cannot add slot ");
PrintKernelInt(slot);
PrintKernelError("\n");
Panic("Termination queue overflow");
}
termination_queue[term_queue_tail] = slot;
term_queue_tail = (term_queue_tail + 1) % MAX_PROCESSES;
term_queue_count++;
}
🤖 Prompt for AI Agents
In Kernel/Process/Process.c around lines 36 to 42, the AddToTerminationQueue
function silently returns when the termination queue is full, risking zombie
processes. Modify the function to log an error or trigger a panic when
term_queue_count reaches MAX_PROCESSES, ensuring that this critical condition is
noticed and handled appropriately.

@assembler-0 assembler-0 merged commit 22fc6bb into main Jul 12, 2025
1 check passed
This was referenced Aug 11, 2025
@coderabbitai coderabbitai bot mentioned this pull request Oct 5, 2025
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