Comments (8)
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
from fiber.
Bug Description
I have a data race, when I call
*fiber.App.Shutdown()
methods and have some unprocessed requests , which handlers uses a request ctxSnippet of data race message:
Previous read at 0x00c00023d1f8 by goroutine 45: github.com/valyala/fasthttp.(*RequestCtx).Done() ~/go/pkg/mod/github.com/valyala/[email protected]/server.go:2726 +0x6a context.(*cancelCtx).propagateCancel.func2() /usr/local/go/src/context/context.go:511 +0x62
How to Reproduce
- Handler that uses request ctx for needed funcs
- Run app with -race flag
- Do some requests to server and start shutdown
- See data race by
*fiber.RequestCtx
Expected Behavior
I think this data race was not in plan
Fiber Version
2.52.4
Code Snippet (optional)
func (h *Handler) CreateTopic(c *fiber.Ctx) error { topic := new(feedback.Topic) if err := c.BodyParser(topic); err != nil { return err } id, err := h.feedbackService.CreateTopic(c.Context(), topic) if err != nil { return err } return c.Status(fiber.StatusCreated).JSON(map[string]string{"id": id}) }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.
You have to use this method, I think, because it works fine for me, and I'm using Go 1.22.1
example:
// waitForShutdownSignal blocks execution until an OS interrupt or SIGTERM signal is received,
// then it gracefully shuts down the Fiber app within the specified timeout.
func waitForShutdownSignal(shutdownTimeout time.Duration, app *fiber.App, db database.Service) {
// Setting up signal capturing.
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
// Blocking until a signal is received.
sig := <-quit
Logger.LogInfof("Shutting down server... reason: %v", sig)
// Start graceful shutdown in a separate goroutine.
go func() {
// Shutdown the Fiber app
if err := app.Shutdown(); err != nil {
Logger.LogErrorf("Server forced to shutdown: %v", err)
}
// Clean up resources and close the database connection
cleanup(db)
}()
// Create a timer that will wait for the shutdownTimeout to elapse
shutdownTimer := time.NewTimer(shutdownTimeout)
defer shutdownTimer.Stop()
// Wait for the timeout to elapse
<-shutdownTimer.C
Logger.LogInfo("Server exiting")
}
logs:
$ go run cmd/api/main.go
2024/03/31 14:44:15 [H0llyW00dzZ] [INFO] Starting server on :8080
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ H0llyW00dzZ โ
โ Fiber v2.52.4 โ
โ http://127.0.0.1:8080 โ
โ (bound on host 0.0.0.0 and port 8080) โ
โ โ
โ Handlers ............ 534 Processes ........... 1 โ
โ Prefork ....... Disabled PID .............. 6860 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2024/03/31 14:44:22 [H0llyW00dzZ] [INFO] Shutting down server... reason: interrupt
2024/03/31 14:44:23 [H0llyW00dzZ] [INFO] Disconnected from database: defaultdb
2024/03/31 14:44:23 [H0llyW00dzZ] [INFO] Database connection closed.
2024/03/31 14:44:27 [H0llyW00dzZ] [INFO] Server exiting
from fiber.
with -race flag:
$ go run cmd/api/main.go -race flag
2024/03/31 15:05:12 [H0llyW00dzZ] [INFO] Starting server on :8080
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ H0llyW00dzZ โ
โ Fiber v2.52.4 โ
โ http://127.0.0.1:8080 โ
โ (bound on host 0.0.0.0 and port 8080) โ
โ โ
โ Handlers ............ 534 Processes ........... 1 โ
โ Prefork ....... Disabled PID ............. 16576 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2024/03/31 15:05:22 [H0llyW00dzZ] [INFO] Shutting down server... reason: interrupt
2024/03/31 15:05:22 [H0llyW00dzZ] [INFO] Disconnected from database: defaultdb
2024/03/31 15:05:22 [H0llyW00dzZ] [INFO] Database connection closed.
2024/03/31 15:05:27 [H0llyW00dzZ] [INFO] Server exiting
from fiber.
Doesn't running with -race
make your program super slow?
from fiber.
Doesn't running with
-race
make your program super slow?
It is only for tests
from fiber.
with -race flag:
$ go run cmd/api/main.go -race flag 2024/03/31 15:05:12 [H0llyW00dzZ] [INFO] Starting server on :8080 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ H0llyW00dzZ โ โ Fiber v2.52.4 โ โ http://127.0.0.1:8080 โ โ (bound on host 0.0.0.0 and port 8080) โ โ โ โ Handlers ............ 534 Processes ........... 1 โ โ Prefork ....... Disabled PID ............. 16576 โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 2024/03/31 15:05:22 [H0llyW00dzZ] [INFO] Shutting down server... reason: interrupt 2024/03/31 15:05:22 [H0llyW00dzZ] [INFO] Disconnected from database: defaultdb 2024/03/31 15:05:22 [H0llyW00dzZ] [INFO] Database connection closed. 2024/03/31 15:05:27 [H0llyW00dzZ] [INFO] Server exiting
I send about 10 requests to a method that uses the request context for further steps, and immediately run the Shutdown method on the interrupt signal (ShutdownWithContext has similar behavior). I accidentally noticed this when app running with the -race flag.
So I can fix this using context package in handler, but without request ctx as a parent
from fiber.
Related Issues (20)
- Explore Performance Optimization: Using a Pool for Memory Allocation in Client Package HOT 1
- Update Documentation and README.md for Client Package Refactor HOT 1
- ๐ [Bug]: local variable based on ctx.Method() is changed in goroutine HOT 5
- ๐ค [Question]: How can i keep a connection alive untill timeout or i response some message? HOT 1
- ๐ค [Question]: Custom Listen Function HOT 3
- CSRF Trusted Origins HOT 2
- ๐งน [Maintenance]: Update RFC Draft for Idempotency Middleware HOT 1
- Have I found a bug in BodyParser, or am I an idiot? HOT 4
- ๐ [Bug]: CORS middleware misclassifies all OPTIONS requests as preflight requests HOT 1
- ๐ค [Question]: Rate limit per path HOT 5
- ๐ค [Question]: How do I get default logger to log error HOT 2
- ๐ค [Question]: how to Get status code in middleware HOT 5
- ๐ [Proposal]: Add Helpers for Internal IP Ranges HOT 4
- ๐ [Bug]: version v2.52.2 up to v2.52.3 , cors error HOT 22
- ๐ [Bug]: serving a compress enabled public folder without +w permissions results in a 404 HOT 3
- ๐ *** CLOSE - I see Context( ) returns this type .... *** - r4[Proposal]: Add RequestCtx in ctx that returns *fasthttp from DefaultCtx HOT 5
- ๐ [Bug]: CORS issue HOT 2
- ๐ [Proposal]: Allow disabling cache for ctx.SendFile() HOT 2
- ๐ค [Question]: Calling other endpoints without network HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fiber.