Skip to content

fix(go-models): harden ModelScope default transport handling - #15339

Merged
JinHai-CN merged 3 commits into
infiniflow:mainfrom
glorydavid03023:go/modelscope-transport-hardening
May 28, 2026
Merged

JinHai-CN merged 3 commits into
infiniflow:mainfrom
glorydavid03023:go/modelscope-transport-hardening

Conversation

@glorydavid03023

@glorydavid03023 glorydavid03023 commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Harden NewModelScopeModel to avoid panics when http.DefaultTransport is a custom non-*http.Transport RoundTripper.
  • Fallback to a safe transport (ProxyFromEnvironment) while preserving existing pooling/timeout settings.
  • Add TestModelScopeNewModelWithCustomDefaultTransport regression coverage.

Test plan

  • go test ./internal/entity/models/ -run TestModelScope -count=1 (cannot run locally: go is unavailable in this environment)

Avoid panicking when http.DefaultTransport is replaced by a custom RoundTripper by falling back to a safe transport, and add regression coverage for the custom-transport case.

Co-authored-by: Cursor <[email protected]>
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label May 28, 2026
@coderabbitai

coderabbitai Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

NewModelScopeModel initialization is hardened to safely clone http.DefaultTransport only when it is an *http.Transport; otherwise it falls back to a new transport with ProxyFromEnvironment. A new test verifies this fallback path.

Changes

HTTP Transport Robustness

Layer / File(s) Summary
Safe HTTP transport initialization and testing
internal/entity/models/modelscope.go, internal/entity/models/modelscope_test.go
NewModelScopeModel guards the clone of http.DefaultTransport with a type assertion; if it is not *http.Transport, a new transport with ProxyFromEnvironment is created. A roundTripperFunc adapter and TestModelScopeNewModelWithCustomDefaultTransport verify the fallback succeeds when http.DefaultTransport is a custom implementation.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested labels

ci, size:L

Poem

🐰 Safe transport takes the guarded way,
Type-checked clones brighten the day,
When defaults shift to stranger shores,
Fallbacks open new doors!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: hardening ModelScope default transport handling to address panic scenarios.
Description check ✅ Passed The description includes a summary of changes and test plan, addressing the template's requirements for context and change type (bug fix).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
internal/entity/models/modelscope_test.go (1)

88-100: ⚡ Quick win

Strengthen the assertion to verify internal fields.

The test only checks if the model is non-nil, but should also verify that model.httpClient and model.httpClient.Transport are non-nil to confirm the fallback path constructs a complete model.

🧪 Proposed fix to strengthen the test assertion
-	if model := NewModelScopeModel(map[string]string{"default": "http://unused"}, URLSuffix{}); model == nil {
-		t.Fatal("NewModelScopeModel returned nil")
+	model := NewModelScopeModel(map[string]string{"default": "http://unused"}, URLSuffix{})
+	if model == nil || model.httpClient == nil || model.httpClient.Transport == nil {
+		t.Fatalf("NewModelScopeModel returned incomplete model: %#v", model)
 	}

Based on learnings: The groq_test.go test for the same scenario verifies model.httpClient and model.httpClient.Transport are non-nil (internal/entity/models/groq_test.go:62-79).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/entity/models/modelscope_test.go` around lines 88 - 100, Update the
test TestModelScopeNewModelWithCustomDefaultTransport to assert not only that
NewModelScopeModel(...) returns non-nil but also that the returned model's
httpClient and httpClient.Transport are non-nil; locate the model created by
calling NewModelScopeModel and add assertions checking model.httpClient != nil
and model.httpClient.Transport != nil (mirroring the checks in groq_test.go) to
ensure the fallback path builds a fully initialized HTTP client.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@internal/entity/models/modelscope_test.go`:
- Around line 88-100: Update the test
TestModelScopeNewModelWithCustomDefaultTransport to assert not only that
NewModelScopeModel(...) returns non-nil but also that the returned model's
httpClient and httpClient.Transport are non-nil; locate the model created by
calling NewModelScopeModel and add assertions checking model.httpClient != nil
and model.httpClient.Transport != nil (mirroring the checks in groq_test.go) to
ensure the fallback path builds a fully initialized HTTP client.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 87bef510-70c1-4f2a-860d-c98b2d0af703

📥 Commits

Reviewing files that changed from the base of the PR and between 8468227 and 4783682.

📒 Files selected for processing (2)
  • internal/entity/models/modelscope.go
  • internal/entity/models/modelscope_test.go

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 28, 2026
@JinHai-CN JinHai-CN added the ci Continue Integration label May 28, 2026
@codecov

codecov Bot commented May 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (7e83643) to head (bf4b549).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #15339   +/-   ##
=======================================
  Coverage   93.16%   93.16%           
=======================================
  Files          10       10           
  Lines         717      717           
  Branches      118      118           
=======================================
  Hits          668      668           
  Misses         29       29           
  Partials       20       20           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants