diff --git a/pkg/instance/proxy.go b/pkg/instance/proxy.go index a61dfb6..df86744 100644 --- a/pkg/instance/proxy.go +++ b/pkg/instance/proxy.go @@ -154,16 +154,8 @@ func (p *proxy) build() (*httputil.ReverseProxy, error) { return proxy, nil } -// serveHTTP handles HTTP requests with inflight tracking and shutting down state checks +// serveHTTP handles HTTP requests with inflight tracking func (p *proxy) serveHTTP(w http.ResponseWriter, r *http.Request) error { - // Check if instance is shutting down - status := p.instance.GetStatus() - if status == ShuttingDown { - w.WriteHeader(http.StatusServiceUnavailable) - w.Write([]byte("Instance is shutting down")) - return fmt.Errorf("instance is shutting down") - } - // Get the reverse proxy reverseProxy, err := p.get() if err != nil { diff --git a/pkg/server/handlers_backends.go b/pkg/server/handlers_backends.go index a60c6ce..1e249f9 100644 --- a/pkg/server/handlers_backends.go +++ b/pkg/server/handlers_backends.go @@ -109,6 +109,12 @@ func (h *Handler) LlamaCppProxy() http.HandlerFunc { return } + // Check if instance is shutting down before autostart logic + if inst.GetStatus() == instance.ShuttingDown { + writeError(w, http.StatusServiceUnavailable, "instance_shutting_down", "Instance is shutting down") + return + } + if !inst.IsRemote() && !inst.IsRunning() { err := h.ensureInstanceRunning(inst) if err != nil { diff --git a/pkg/server/handlers_openai.go b/pkg/server/handlers_openai.go index 0937e6a..81aa9e7 100644 --- a/pkg/server/handlers_openai.go +++ b/pkg/server/handlers_openai.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "io" + "llamactl/pkg/instance" "llamactl/pkg/validation" "net/http" ) @@ -106,6 +107,12 @@ func (h *Handler) OpenAIProxy() http.HandlerFunc { return } + // Check if instance is shutting down before autostart logic + if inst.GetStatus() == instance.ShuttingDown { + writeError(w, http.StatusServiceUnavailable, "instance_shutting_down", "Instance is shutting down") + return + } + if !inst.IsRemote() && !inst.IsRunning() { err := h.ensureInstanceRunning(inst) if err != nil {