Description
Bug Description
The Immutable
configuration option in Fiber, when set to true, is intended to ensure that certain request values (e.g., request bodies) are immutable and accessible beyond the handler's lifecycle. However, enabling this option does not work as expected, resulting in data race errors when the request body is processed within a differernt goroutine.
Documentation Says:
When set to true, this relinquishes the 0-allocation promise in certain cases in order to access the handler values (e.g., request bodies) in an immutable fashion so that these values are available even if you return from the handler.
Am I missing something here, or is this the expected behavior, and I need to manually copy the body to ensure immutability in my code?
How to Reproduce
- Create a Fiber app with the Immutable and StreamRequestBody configurations set to true
app := fiber.New(fiber.Config{
Immutable: true,
StreamRequestBody: true,
})
- Define a route where the request body is processed in new goroutine:
app.Get("/keep", func(c *fiber.Ctx) error {
body := c.Body() // Supposed to be immutable
go func() {
for i := 0; i < 100; i++ {
time.Sleep(time.Second)
var data map[string]any
err := json.Unmarshal(body, &data)
fmt.Println(err, data) // Accessing body in a goroutine
}
}()
return c.JSON(map[string]any{"success": true})
})
- send multiple concurrent requests
curl --request GET 'http://localhost:8080/keep' \
--header 'Content-Type: application/json' \
--data-raw '{"key": "val"}'
Expected Behavior
The request body c.Body()
should remain immutable and safe to access across goroutines without causing data races but actually get a data races
==================
WARNING: DATA RACE
Read at 0x00c00011a080 by goroutine 76:
encoding/json.checkValid()
...
Previous write at 0x00c00011a080 by goroutine 75:
runtime.slicecopy()
...
==================
Fiber Version
v2.52.5
Checklist:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my problem prior to opening this one.
- I understand that improperly formatted bug reports may be closed without explanation.