<div dir="ltr">I'm a new developer/user for Varnish, and was looking to extend a module with some regex support (vmod_cookie if you must know). I don't know anything about vmod development or Varnish threading, so to get an idea of how to do this I looked at the vmod_header module.<div><br></div><div>But I'm confused about why they thought they needed a mutex to protected a call to VRT_re_init. Is this just a useless, historic thing that got left in for some reason?</div><div><br></div><div><a href="https://github.com/varnish/varnish-modules/blob/master/src/vmod_header.c">https://github.com/varnish/varnish-modules/blob/master/src/vmod_header.c</a><br></div><div><br></div><div>If you look at vmod_remove it has a 2nd parameter that's in the vcc as PRIV_CALL. This parameter is initialized with the string value from the last parameter (a regex pattern). It does this by calling <font color="#005cc5" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace"><span style="font-size:12px;white-space:pre">header_init_re()</span></font>. And if you look at that function you see:</div><div><br></div><div><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container" style="box-sizing:border-box;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,system-ui,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-size:14px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><tbody style="box-sizing:border-box"><tr style="box-sizing:border-box"></tr><tr style="box-sizing:border-box"><td id="gmail-LC48" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre"><span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"><span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)">/*</span></span></td></tr><tr style="box-sizing:border-box"><td id="gmail-L49" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC49" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre"><span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"> * Initialize the regex *s on priv, if it hasn't already been done.</span></td></tr><tr style="box-sizing:border-box"><td id="gmail-L50" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC50" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre"><span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"> * XXX: We have to recheck the condition after grabbing the lock to avoid a</span></td></tr><tr style="box-sizing:border-box"><td id="gmail-L51" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC51" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre"><span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"> * XXX: race condition.</span></td></tr><tr style="box-sizing:border-box"><td id="gmail-L52" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC52" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre"><span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)"> <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(106,115,125)">*/</span></span></td></tr><tr style="box-sizing:border-box"><td id="gmail-L53" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC53" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">static</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">void</span></td></tr><tr style="box-sizing:border-box"><td id="gmail-L54" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC54" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre"><span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">header_init_re</span>(<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">struct</span> vmod_priv *priv, <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">const</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">char</span> *s)</td></tr><tr style="box-sizing:border-box"><td id="gmail-L55" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC55" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">{</td></tr><tr style="box-sizing:border-box"><td id="gmail-L56" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC56" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">   <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">if</span> (priv-><span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(36,41,46)">priv</span> == <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">NULL</span>) {</td></tr><tr style="box-sizing:border-box"><td id="gmail-L57" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC57" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">          <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">assert</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">pthread_mutex_lock</span>(&header_mutex) == <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span>);</td></tr><tr style="box-sizing:border-box"><td id="gmail-L58" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC58" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">            <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">if</span> (priv-><span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(36,41,46)">priv</span> == <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">NULL</span>) {</td></tr><tr style="box-sizing:border-box"><td id="gmail-L59" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC59" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">                  <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">VRT_re_init</span>(&priv-><span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(36,41,46)">priv</span>, s);</td></tr><tr style="box-sizing:border-box"><td id="gmail-L60" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC60" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">                      priv-><span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(36,41,46)">free</span> = VRT_re_fini;</td></tr><tr style="box-sizing:border-box"><td id="gmail-L61" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC61" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">                }</td></tr><tr style="box-sizing:border-box"><td id="gmail-L62" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC62" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">               <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">pthread_mutex_unlock</span>(&header_mutex);</td></tr><tr style="box-sizing:border-box"><td id="gmail-L63" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC63" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">     }</td></tr><tr style="box-sizing:border-box"><td id="gmail-L64" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td><td id="gmail-LC64" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;color:rgb(36,41,46);word-wrap:normal;white-space:pre">}</td></tr><tr style="box-sizing:border-box"><td id="gmail-L65" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.3);text-align:right;white-space:nowrap;vertical-align:top"></td></tr></tbody></table>My reading of the docs makes me think that this "priv" only lives for each single call of the function. There should be no reason to protect it with a global mutex. Do the internals of VRT_re_init() require this? I can't imagine how, because if it did then this method of protecting it is broken anyway.</div><div><br></div><div>I suspect this is leftover from some rewrite or updating of the module but wanted to check.<br class="gmail-Apple-interchange-newline"><br></div></div>