Skip to content

fix: race during stop of active backends #5106

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 2 commits into from
Mar 31, 2025
Merged

Conversation

mudler
Copy link
Owner

@mudler mudler commented Mar 31, 2025

Description

This PR is a small refactor while I was walking out the code, and found a potential race condition.

This pull request introduces several changes to improve the handling of backend shutdowns and model loading in the pkg/model package. The key changes include adding a new environment variable to force backend shutdown, refactoring the deleteProcess method, and ensuring proper backend management during model loading.

Backend shutdown improvements:

  • .env: Added a new environment variable LOCALAI_FORCE_BACKEND_SHUTDOWN to force the shutdown of backends if they are busy.
  • pkg/model/process.go: Refactored the deleteProcess method to handle retries and force shutdown if the backend is busy, using the new environment variable. [1] [2]

Model loading enhancements:

  • pkg/model/initializers.go: Modified the stopActiveBackends method to return early if singleActiveBackend is not set, and added comments to clarify the backend loading process. [1] [2] [3]
  • pkg/model/loader.go: Removed the busy-wait loop from the ShutdownModel method, delegating the shutdown process to the deleteProcess method.

Notes for Reviewers

Signed commits

  • Yes, I signed my commits.

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Copy link

netlify bot commented Mar 31, 2025

Deploy Preview for localai ready!

Name Link
🔨 Latest commit ce703bf
🔍 Latest deploy log https://app.netlify.com/sites/localai/deploys/67eafee2eee60f00087af45e
😎 Deploy Preview https://deploy-preview-5106--localai.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 site configuration.

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler mudler changed the title chore: drop double call to stop all backends, refactors fix: race during stop of active backends Mar 31, 2025
@mudler mudler requested a review from Copilot March 31, 2025 20:46
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses a race condition during the shutdown of active backends by refactoring the shutdown logic. Key changes include refactoring the deletion logic in deleteProcess with a retry loop, replacing the old ShutdownModel in loader.go with deleteProcess, and refining stopActiveBackends behavior based on the singleActiveBackend flag.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
pkg/model/process.go Refactored deleteProcess to include a retry loop and forced shutdown
pkg/model/loader.go Removed outdated shutdown logic and now calls deleteProcess
pkg/model/initializers.go Adjusted stopActiveBackends logic for proper single backend handling
Files not reviewed (1)
  • .env: Language not supported

@@ -44,9 +62,12 @@ func (ml *ModelLoader) deleteProcess(s string) error {

func (ml *ModelLoader) StopGRPC(filter GRPCProcessFilter) error {
var err error = nil
ml.mu.Lock()
Copy link
Owner Author

Choose a reason for hiding this comment

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

here we were missing the lock when iterating over models

@mudler mudler merged commit 05f7004 into master Mar 31, 2025
27 checks passed
@mudler mudler deleted the chore/minor_refactors branch March 31, 2025 22:01
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