Closed sujit-baniya closed 3 weeks ago
Have we changed our strategy? Is adding a route after the server start possible in v3, didn't know that.
@ReneWerner87 Yes, it was added here with a big warning. https://github.com/gofiber/fiber/pull/3074
Ok then its a bug. Seems that this method is not 100% thread safe.
It is not, thus why the warning. Basically that last test hit the server while the route was being added to the Router.
Or the access for the routing items is not thread safe, but adding a mutex here will slow down it for everyone.
Or the access for the routing items is not thread safe, but adding a mutex here will slow down it for everyone.
Yes correct.
However, we could make a switch in the config called dynamicRouting
and only use the mutex if it is set to true.
Then the others would not run slower and you could only use mutex in the routing process and also in rebuildRouting if this is set. This would also make rebuild tree faster for those who do not use dynamic routing.
@sujit-baniya Found the problem in your code, the handler being created inside your first handler is using the context
from the parent handler. The context is only valid during the duration of the request.
Change your code to this:
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {
app.Get("/hello", func(ctx fiber.Ctx) error {
// Use ctx not c here
return ctx.SendString("Hello, World 👋!")
})
app.RebuildTree()
return c.SendString("Added")
})
app.Listen(":3000")
}
I added a comment where the issue was.
Ah! ok... I'm attempting few more times before closing this issue.
By the way, I was expecting this code to work but after visiting /updated
I expect to get "Bye, World 👋!"
but received "Hello, World 👋!"
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {
app.Get("/hello", func(ctx fiber.Ctx) error {
// Use ctx not c here
return ctx.SendString("Hello, World 👋!")
})
app.RebuildTree()
return c.SendString("Added")
})
app.Get("/updated", func(c fiber.Ctx) error {
app.Get("/hello", func(ctx fiber.Ctx) error {
// Use ctx not c here
return ctx.SendString("Bye, World 👋!")
})
app.RebuildTree()
return c.SendString("Updated")
})
app.Listen(":3000")
}
@sujit-baniya You can't overwrite or remove routes.
Bug Description
Video:
https://github.com/user-attachments/assets/235a1bb4-00b9-45ce-a9e3-cd8eef2f19b8
In this code, I just tried to add a new route and rebuild the route stack. When trying to visit the newly added route, the error is thrown which is random in nature.
Error:
Error Code:
How to Reproduce
Steps to reproduce the behavior:
Run following code, at random there should be such error:
Expected Behavior
Application should have executed without any issue
Fiber Version
v3.0.0-beta.3
Code Snippet (optional)
Checklist: