Skip to content

Conversation

@chojs23
Copy link

@chojs23 chojs23 commented Nov 11, 2025

What?

When a request fails, the Response object is still returned but previously the Headers and Cookies maps were nil.

Why?

Trying to assign to a nil map causes a panic. Now these maps are always initialized, so users can safely modify them even for failed requests.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

Closes #5213

@chojs23 chojs23 requested a review from a team as a code owner November 11, 2025 01:05
@chojs23 chojs23 requested review from AgnesToulet and inancgumus and removed request for a team November 11, 2025 01:05
@inancgumus
Copy link
Contributor

Hi @chojs23, thanks for the contribution!

Can you add a test to reproduce this issue to avoid future regressions?

When a request fails, the Response object is still returned but previously the Headers and Cookies maps were nil. Trying to assign to a nil map causes a panic. Now these maps are always initialized, so users can safely modify them even for failed requests.

@inancgumus inancgumus temporarily deployed to azure-trusted-signing November 11, 2025 20:23 — with GitHub Actions Inactive
@inancgumus inancgumus temporarily deployed to azure-trusted-signing November 11, 2025 20:25 — with GitHub Actions Inactive
@chojs23 chojs23 force-pushed the fix/set-header-panic branch from 98fb1bf to 10bbb87 Compare November 12, 2025 01:29
@chojs23
Copy link
Author

chojs23 commented Nov 12, 2025

Hi @inancgumus, I added some tests.

@mstoykov mstoykov added this to the v1.5.0 milestone Nov 17, 2025
@chojs23 chojs23 temporarily deployed to azure-trusted-signing November 18, 2025 20:13 — with GitHub Actions Inactive
@chojs23 chojs23 temporarily deployed to azure-trusted-signing November 18, 2025 20:15 — with GitHub Actions Inactive
@chojs23 chojs23 temporarily deployed to azure-trusted-signing November 19, 2025 15:29 — with GitHub Actions Inactive
@chojs23 chojs23 temporarily deployed to azure-trusted-signing November 19, 2025 15:31 — with GitHub Actions Inactive
Copy link
Contributor

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

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

Thanks for adding the tests. All LGTM but one thing.

Comment on lines +232 to +237
resp := &Response{
URL: preq.URL.URL,
Request: respReq,
Headers: make(map[string]string),
Cookies: make(map[string][]*HTTPCookie),
}
Copy link
Contributor

@inancgumus inancgumus Nov 19, 2025

Choose a reason for hiding this comment

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

Can you use the NewResponse function here? Other than that, your PR is LGTM.

Copy link
Author

Choose a reason for hiding this comment

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

@inancgumus Thanks for review!

Copy link
Contributor

Choose a reason for hiding this comment

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

This is actually breaking a test because we expect the body to be null here: https://github.com/grafana/k6/blob/master/js/modules/k6/http/request_test.go#L2256 so I think this should be added in both places.

Copy link
Contributor

Choose a reason for hiding this comment

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

@chojs23, sure. Can you check @AgnesToulet's suggestion please?

Copy link
Author

Choose a reason for hiding this comment

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

@inancgumus Creating Response with NewResponse function initialize body with empty array but this could break TestBinaryResponseWithStatus0 test as @AgnesToulet mentioned.

Hmm, Can we change the initial body null to empty array? I don't know the original code that creating Response with nil body was intended.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to keep the original behavior as users could rely on checking if the response body is null in some cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

To keep the original behavior, can you revert the last commit? Thank you!

Copy link
Author

Choose a reason for hiding this comment

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

Sure, I reverted it.

@chojs23 chojs23 temporarily deployed to azure-trusted-signing November 20, 2025 14:38 — with GitHub Actions Inactive
@chojs23 chojs23 temporarily deployed to azure-trusted-signing November 20, 2025 14:40 — with GitHub Actions Inactive
@chojs23 chojs23 force-pushed the fix/set-header-panic branch from 115af52 to 022acbb Compare November 22, 2025 00:57
@chojs23 chojs23 temporarily deployed to azure-trusted-signing November 22, 2025 02:58 — with GitHub Actions Inactive
@chojs23 chojs23 deployed to azure-trusted-signing November 22, 2025 03:00 — with GitHub Actions Active
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.

Panic when assining not a map on an null object

4 participants