darkweak / souin

An HTTP cache system, RFC compliant, compatible with @tyktechnologies, @traefik, @caddyserver, @go-chi, @bnkamalesh, @beego, @devfeel, @labstack, @gofiber, @go-goyave, @go-kratos, @gin-gonic, @roadrunner-server, @zalando, @zeromicro, @nginx and @apache

Home Page:https://docs.souin.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fatal error: concurrent map writes

zx8 opened this issue · comments

commented

I am testing out Souin as a Caddy plugin with the following Caddyfile:

{
  cache {
    ttl 10s
    allowed_http_verbs GET
    nuts {
      path /tmp/cache
    }
  }
  order cache before rewrite
}

http://127.0.0.1:8080 {
  cache {
    timeout {
      backend 20s
    }
  }
  request_header -Cache-Control
  request_header Content-Encoding gzip
  header >Cache-Control public
  header -Pragma
  header -Expires
  reverse_proxy http://127.0.0.1:3000 {
    transport http {
      read_timeout 20s
      response_header_timeout 20s
    }
  }
}

(I'm experimenting with the Cache-Control header behavior, so ignore the fact that things look a little strange at the moment.)

I set up a simple server on port 3000 that responds with a JSON payload, with a delay of 3 seconds to simulate network latency. I then reverse proxied traffic to this server, sending 10 requests in parallel to my Caddy listener on port 8080.

After a few requests, my Caddy server crashed with a "concurrent map writes" error:

2023/10/30 13:40:58.596 INFO  http.handlers.cache Reused response from concurrent request with the key GET-http-localhost:8080-/data
2023/10/30 13:41:08.586 INFO  http.handlers.cache Reused response from concurrent request with the key GET-http-localhost:8080-/data
2023/10/30 13:41:08.594 INFO  http.handlers.cache Reused response from concurrent request with the key GET-http-localhost:8080-/data
2023/10/30 13:41:08.602 INFO  http.handlers.cache Reused response from concurrent request with the key GET-http-localhost:8080-/data
2023/10/30 13:41:18.184 INFO  http.handlers.cache Reused response from concurrent request with the key GET-http-localhost:8080-/data
2023/10/30 13:41:26.908 INFO  http.handlers.cache Reused response from concurrent request with the key GET-http-localhost:8080-/data
2023/10/30 13:41:26.908 INFO  http.handlers.cache Reused response from concurrent request with the key GET-http-localhost:8080-/data
fatal error: concurrent map writes

Hey @zx8 thank you for your bug report. I'm currently on it, pretty simple to reproduce but sadly hard to fix. I keep my fingers crossed to have a working fix for that as soon as possible. 🤞

Hello @zx8 the commit c9004d66d1a368fe394958acbdbba8581c8f99b4 solves the race conditions
xcaddy build --with github.com/darkweak/souin/plugins/caddy@c9004d66d1a368fe394958acbdbba8581c8f99b4 --with github.com/darkweak/souin@c9004d66d1a368fe394958acbdbba8581c8f99b4