-
Notifications
You must be signed in to change notification settings - Fork 131
Fix panic in SGLang proxy handling of concurrent requests #632
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /* | ||
| Copyright 2025 The llm-d Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package proxy | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/llm-d/llm-d-inference-scheduler/pkg/common" | ||
| . "github.com/onsi/ginkgo/v2" // nolint:revive | ||
| . "github.com/onsi/gomega" // nolint:revive | ||
| ) | ||
|
|
||
| var _ = Describe("SGLang Connector", func() { | ||
|
|
||
| var testInfo *sidecarTestInfo | ||
|
|
||
| BeforeEach(func() { | ||
| // Mock testing setup using the SGLang connector mode | ||
| testInfo = sidecarConnectionTestSetup(ConnectorSGLang) | ||
| }) | ||
|
|
||
| It("should successfully send concurrent requests to prefill and decode with bootstrap info", func() { | ||
| By("starting the proxy") | ||
| go func() { | ||
| defer GinkgoRecover() | ||
|
|
||
| validator := &AllowlistValidator{enabled: false} | ||
| err := testInfo.proxy.Start(testInfo.ctx, nil, validator) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| testInfo.stoppedCh <- struct{}{} | ||
| }() | ||
|
|
||
| // Wait for proxy to start | ||
| time.Sleep(1 * time.Second) | ||
| Expect(testInfo.proxy.addr).ToNot(BeNil()) | ||
| proxyBaseAddr := "http://" + testInfo.proxy.addr.String() | ||
|
|
||
| By("sending a /v1/chat/completions request with prefill header") | ||
| body := `{ | ||
| "model": "Qwen/Qwen2-0.5B", | ||
| "messages": [ | ||
| {"role": "user", "content": "Hello"} | ||
| ], | ||
| "max_tokens": 50 | ||
| }` | ||
|
|
||
| req, err := http.NewRequest(http.MethodPost, proxyBaseAddr+ChatCompletionsPath, strings.NewReader(body)) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| prefillHostPort := testInfo.prefillBackend.URL[len("http://"):] | ||
| req.Header.Add(common.PrefillPodHeader, prefillHostPort) | ||
|
|
||
| rp, err := http.DefaultClient.Do(req) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| if rp.StatusCode != 200 { | ||
| bp, _ := io.ReadAll(rp.Body) //nolint:all | ||
| Fail(string(bp)) | ||
| } | ||
|
|
||
| // Because SGLang connector sends requests concurrently (prefill in goroutine), | ||
| // we sleep a tiny bit to ensure the prefill handler has time to finish processing. | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| // Validate prefill request | ||
| Expect(testInfo.prefillHandler.RequestCount.Load()).To(BeNumerically("==", 1)) | ||
| Expect(testInfo.prefillHandler.CompletionRequests).To(HaveLen(1)) | ||
| prq1 := testInfo.prefillHandler.CompletionRequests[0] | ||
|
|
||
| // Validate decode request | ||
| Expect(testInfo.decodeHandler.RequestCount.Load()).To(BeNumerically("==", 1)) | ||
| Expect(testInfo.decodeHandler.CompletionRequests).To(HaveLen(1)) | ||
| drq1 := testInfo.decodeHandler.CompletionRequests[0] | ||
|
|
||
| // Bootstrap validations for prefill | ||
| Expect(prq1).To(HaveKey(requestFieldBootstrapHost)) | ||
| Expect(prq1).To(HaveKey(requestFieldBootstrapPort)) | ||
| Expect(prq1).To(HaveKey(requestFieldBootstrapRoom)) | ||
|
|
||
| expectedHost := strings.Split(prefillHostPort, ":")[0] | ||
| Expect(prq1[requestFieldBootstrapHost]).To(Equal(expectedHost)) | ||
| Expect(prq1[requestFieldBootstrapPort]).To(Equal(float64(sglangBootstrapPort))) | ||
| Expect(prq1[requestFieldBootstrapRoom]).ToNot(BeNil()) | ||
|
|
||
| // Bootstrap validations for decode | ||
| Expect(drq1).To(HaveKey(requestFieldBootstrapHost)) | ||
| Expect(drq1).To(HaveKey(requestFieldBootstrapPort)) | ||
| Expect(drq1).To(HaveKey(requestFieldBootstrapRoom)) | ||
|
|
||
| Expect(drq1[requestFieldBootstrapHost]).To(Equal(expectedHost)) | ||
| Expect(drq1[requestFieldBootstrapPort]).To(Equal(float64(sglangBootstrapPort))) | ||
| Expect(drq1[requestFieldBootstrapRoom]).To(Equal(prq1[requestFieldBootstrapRoom])) // Room ID must match | ||
|
|
||
| testInfo.cancelFn() | ||
| <-testInfo.stoppedCh | ||
| }) | ||
|
|
||
| It("should not panic when prefill response is slower than decode response", func() { | ||
| // Stop previously injected servers | ||
| testInfo.decodeBackend.Close() | ||
| testInfo.prefillBackend.Close() | ||
|
|
||
| var prefillFinished bool | ||
|
|
||
| slowPrefill := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| testInfo.prefillHandler.ServeHTTP(w, r) | ||
| time.Sleep(300 * time.Millisecond) // Simulated load delay on KV Cache | ||
| prefillFinished = true | ||
| }) | ||
| testInfo.prefillBackend = httptest.NewServer(slowPrefill) | ||
|
|
||
| fastDecode := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| testInfo.decodeHandler.ServeHTTP(w, r) | ||
| }) | ||
| testInfo.decodeBackend = httptest.NewServer(fastDecode) | ||
| testInfo.decodeURL, _ = url.Parse(testInfo.decodeBackend.URL) | ||
|
|
||
| // Re-initialize proxy to fetch the new mock addresses | ||
| cfg := Config{ | ||
| Connector: ConnectorSGLang, | ||
| } | ||
| testInfo.proxy = NewProxy("0", testInfo.decodeURL, cfg) | ||
|
|
||
| go func() { | ||
| defer GinkgoRecover() | ||
| validator := &AllowlistValidator{enabled: false} | ||
| err := testInfo.proxy.Start(testInfo.ctx, nil, validator) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| testInfo.stoppedCh <- struct{}{} | ||
| }() | ||
|
|
||
| time.Sleep(1 * time.Second) | ||
| proxyBaseAddr := "http://" + testInfo.proxy.addr.String() | ||
|
|
||
| body := `{"model": "Qwen", "messages": [{"role": "user", "content": "Hello"}], "max_tokens": 50}` | ||
| req, err := http.NewRequest(http.MethodPost, proxyBaseAddr+ChatCompletionsPath, strings.NewReader(body)) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| prefillHostPort := testInfo.prefillBackend.URL[len("http://"):] | ||
| req.Header.Add(common.PrefillPodHeader, prefillHostPort) | ||
|
|
||
| // Submit request. This will complete as soon as fastDecode completes. | ||
| rp, err := http.DefaultClient.Do(req) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(rp.StatusCode).To(Equal(200)) | ||
|
|
||
| // The original panicking goroutine takes 300ms total. Give it time to attempt finishing up! | ||
| time.Sleep(500 * time.Millisecond) | ||
|
|
||
| Expect(prefillFinished).To(BeTrue()) | ||
| Expect(testInfo.prefillHandler.RequestCount.Load()).To(BeNumerically("==", 1)) | ||
| Expect(testInfo.decodeHandler.RequestCount.Load()).To(BeNumerically("==", 1)) | ||
|
|
||
| testInfo.cancelFn() | ||
| <-testInfo.stoppedCh | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.