gin bug

客户端两个接口同时访问 ginweb 打印得知两个接口的context一样 抓包得知第二个接口的返回包含了第一个接口的返回值 第一个接口返回code 200 body 空 抓包显示 还是两个tcp连接 竟然出现这种问题

偶现

8:2 正常 vs 不正常 概率还挺高

确实是gin 的bug

// ServeHTTP conforms to the http.Handler interface.
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    c := engine.pool.Get().(*Context)
    c.writermem.reset(w)
    c.Request = req
    c.reset()

    engine.handleHTTPRequest(c)

    engine.pool.Put(c)
}

// HandleContext re-enter a context that has been rewritten.
// This can be done by setting c.Request.URL.Path to your new target.
// Disclaimer: You can loop yourself to death with this, use wisely.
func (engine *Engine) HandleContext(c *Context) {
    c.reset()
    engine.handleHTTPRequest(c)
    engine.pool.Put(c)
}



v1.6.3 修复的gin源码

// HandleContext re-enter a context that has been rewritten.
// This can be done by setting c.Request.URL.Path to your new target.
// Disclaimer: You can loop yourself to death with this, use wisely.
func (engine *Engine) HandleContext(c *Context) {
    oldIndexValue := c.index
    c.reset()
    engine.handleHTTPRequest(c)

    c.index = oldIndexValue
}

您可以自由的转载和修改,但请务必注明文章来源并且不可用于商业目的。
本站大部分内容收集于互联网,如果有侵权内容、不妥之处,请联系删除。敬请谅解!

已有 2 条评论

  1. 简述:gin使用了sync.pool来保存context增加性能 老版本的handlecontext重定向操作会造成连续put两个一样的context到pool中 因此新请求有概率 c := engine.pool.Get().(*Context) get到相同地址的context造成该bug

添加新评论

  关于博主【WANG-FEiHU】

Duplicate
-----------Complicate
--------------------------Appreciate
-----------------------------------------Fate

闻先后,术专攻
三人有师
习得文武艺,货与帝王家
人性不曾变,资本永无眠

-----------花有重开日,人无再少年-----------

  分类目录

  monitor(TD)

暗自伤心,不如立即行动。

再多一点努力,就多一点成功 的 可能

得意淡然,失意坦然;喜而不狂,忧而不伤。

海纳百川,有容乃大;壁立千仞,无欲则刚。

自古华山一条路,心不狠,站不稳

经历的越多才越明白,古人诚不欺我