echo: Race on echo.maxParams, unable to use some context methods safely concurrently
Issue Description
Hi there!
I’ve been using echo for a relatively large api service, upgrading from 3.x to the latest 4.x recently and fixing some issues along the way.
While the initial issue I ran into may be related to #1678 (some groups or routes randomly “forget” their handler and start returning 404’s) I discovered some other issues in echo.
First I’d like to thank the maintainers for the project, I don’t mind discussing this issue or attempting to contribute a fix.
Anyways, I noticed here a significant race condition introduced via #1535 .
echo.maxParam cannot be changed safely, even with atomics (which would fix the data race), as the algorithm itself is racy.
Overwriting that value via the context I realized was causing panics here for me (not sure why that isn’t just using the len of pvalues to begin with).
Further, manipulation of the context/echo types seems to lead to issues with the Find algorithm in the Router which is already hard to follow, and may be leading to my initial issue above.
Perhaps unrelated, is there a particular usage for the context type and methods I’m missing? It’s defined as an interface but cannot be (re)implemented due to unsafe assertions such as this. I was hoping to override/shadow some methods for debugging purposes but cannot.
Thanks again, as for the initial issue I mentioned above regarding 404’s I’d like to discuss/tackle after this one.
Checklist
- [ x] Dependencies installed
- Latest Go
- [ x] No typos
- [x ] Searched existing issues and docs
Expected behaviour
Safely use context methods
Actual behaviour
Panic from incorrect len / race.
Steps to reproduce
Noted above.
Version/commit
Latest echo release, v4.1.17. Note I haven’t tried with master, I noticed ~50 commits to master since the last release, I briefly looked at them but didn’t see anything that would fix this.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 24 (14 by maintainers)
I really can not say what intentions @vishr had but
echo.Echofields are public sincev3. Probably assumptions is that getters are annoying when 99.9% cases users do not change things after server has started - and this is decision, whether we agree or not, that can not be changed withinv4. This is not even limited to public fields - you can calle.Useand have datarace with middlewares as their slice is accessed inhttp.Server.ServeHTTP()coroutine when handling clients request.Basically - trade-off is that for ease of use you must not mutate anything related to running Echo and Router instance.
This is not something that is specific to Echo. As Echo and Gin are quite similar both have similar limitation (having public fields which are accessed or accessible from context inside handler coroutines) - they are not meant to be mutated after server has been started.
Personally - router not being safe after server has started has been bothering me from design perspective but addressing everything related to potentially dataracey parts is questionable, as there are not much demand for it (adding routes etc after startup) and this would change the API quite drastically (no public fields anymore) - which would rub people, who do not need this feature, wrong way and they seem to be vast majority.
#2611 addresses this issue. middlewares using
SetParamValuesare now in better place.coming back to
maxParamand Context we probably can remove references tomaxParamthere and allow it grow if needed bySetParamValuesand not to shrink. And reset it to current capacity and we are good to go.assuming it can only grow we do not need to access
echo.Echo.maxParamfield.