Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 [Bug]: local variable based on ctx.Method() is changed in goroutine #2902

Closed
3 tasks done
maxwww opened this issue Mar 7, 2024 · 5 comments
Closed
3 tasks done

Comments

@maxwww
Copy link

maxwww commented Mar 7, 2024

Bug Description

I noticed some strange behavior during implementing a middleware.
If I create a variable in middleware scope which holds request method and then pass the variable to a goroutine
method := ctx.Method()
The variable can be modified if there is parallel request.
In my case I got strange value "GETT" when I expect "POST"

How to Reproduce

I prepared small program to reproduce the issue here https://go.dev/play/p/u4BlH1khRTG
or here https://github.com/maxwww/fibertmp/blob/main/main.go
Just run the program
go run main.go

Expected Behavior

I expect to see correct value of variable
ER: "POST"
AR: "GETT"

Fiber Version

v2.52.2

Code Snippet (optional)

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	fiber "github.com/gofiber/fiber/v2"
)

func main() {
	app := fiber.New()
	app.Use(func(ctx *fiber.Ctx) error {
		id := fmt.Sprintf("%d", time.Now().UnixNano())
		method1 := ctx.Method()
		method2 := fmt.Sprintf("%s", ctx.Method())

		if err := ctx.Next(); err != nil {
			return err
		}

		go func(id, method1, method2 string) {
			time.Sleep(1 * time.Second)
			fmt.Printf("==========\n")
			fmt.Printf("id -> %s\n", id)
			fmt.Printf("method1 -> %s\n", method1)
			fmt.Printf("method2 -> %s\n", method2)
		}(id, method1, method2)
		return nil
	})
	app.Post("/", func(ctx *fiber.Ctx) error {
		log.Println("in GET")
		return ctx.SendString("hello from post")
	})
	app.Get("/", func(ctx *fiber.Ctx) error {
		log.Println("in POST")
		return ctx.SendString("hello from get")
	})

	req, err := http.NewRequest("POST", "/", nil)
	if err != nil {
		log.Panic(err)
		return
	}

	_, err = app.Test(req, 1)
	if err != nil {
		log.Panic(err)
	}

	req, err = http.NewRequest("GET", "/", nil)
	if err != nil {
		log.Panic(err)
		return
	}

	_, err = app.Test(req, 1)
	if err != nil {
		log.Panic(err)
	}

	time.Sleep(2 * time.Second)
}

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.
Copy link

welcome bot commented Mar 7, 2024

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

its because of the zero allocation concept

image
https://docs.gofiber.io/#zero-allocation

@ReneWerner87
Copy link
Member

we just forgot to mention it for this method

@maxwww
Copy link
Author

maxwww commented Mar 7, 2024

Thanks!

@maxwww
Copy link
Author

maxwww commented Mar 7, 2024

Closed as not a bug

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

No branches or pull requests

2 participants