# Cache::Memcached 1.21が突如クラッシュした - URL: https://www.drk7.jp/MT/archives/001280.html - 公開: 2007.11.02 - カテゴリ: Perl言語 - 著者: drk ![Cache::Memcached 1.21が突如クラッシュしたのイメージ(挿絵)](https://www.drk7.jp/media/opt/posts/001280/illust.jpg.w1600.webp) お仕事で使っているサーバが突如落ちた。データセンターまで 30 分ほどかかるので、リモートハンドサービスってのでリブートして頂いてますが、それでも 20 分くらいかかります。正直不便。 まぁその話はおいておいて、どうにも腑に落ちないのですが、ログを見る限りエラーを吐いているのは Cache::Memcached らしい。 ## 半年安定していたのに突然のエラー ってか、半年ほど前にこの業務アプリに Cache::Memcached を入れて実装した機能があるんですが、その間安定して動いていた。 が、突如としてこんなエラーを吐きやがった。 ``` Use of uninitialized value in vec at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 722. Use of uninitialized value in vec at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 737. send() on closed socket $sock at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 688. Use of uninitialized value in numeric gt (>) at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 692. Use of uninitialized value in vec at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 360. Use of uninitialized value in vec at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 367. send() on closed socket $sock at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 368. Use of uninitialized value in numeric gt (>) at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 371. Use of uninitialized value in string eq at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 492. Use of uninitialized value in vec at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 360. Use of uninitialized value in vec at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 367. send() on closed socket $sock at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 368. Use of uninitialized value in numeric gt (>) at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 371. Use of uninitialized value in string eq at /usr/local/lib/perl5/site_perl/5.8.6/Cache/Memcached.pm line 492. ``` エラーを吐くタイミングで、httpd プロセスが異常なほどメモリを食いまくる。メモリリークしたときの動きに似てる。で、肥大した httpd プロセスが数個立ち上がって、swap して OS フリーズ。こんな症状でした。良く判らないけど mysql のプロセスまで肥大しやがった。全く意味不明。 ## Cache::Memcached を update したら治まった いろいろ切り分けして、mysql 止めてみたり memcached を再起動しても症状変わらずで、何度もサーバが固まる。取りあえず今までの経験則から Cache::Memcached を update してみると、症状は治まった。 むむむぅぅぅ・・・謎だけど、使っていた Cache::Memcached は 1.21 で、1.24 に update された。ChangeLog みてみる。 > 2007-07-17: version 1.24 > > - update the stats method, including tests for it (Ronald J Kimball ) > - arguments to 'new' constructor can be %hash or $hashref now (previously was only $hashref) > - work around a Perl segfault (Matthieu PATOU ) see > > 2007-06-19: version 1.23 > > - add 'remove' as an alias for 'delete' (Dave Cardwell ) > > 2007-06-18: version 1.22 > > - lost connection handling broken due to wrong %sock_map indexing fix from RHESA > - let parser_class be configured as a constructor option, defaulting to XS if available, else regular. (unless $ENV{NO_XS} is set, in which case the default is regular) うーん・・・1.22 になんかソレっぽい bug-fix がある。これ。 > lost connection handling broken due to wrong %sock_map indexing ## 1.21 の %sock_map のバグ ちょっと調べてみると、ここらへんが情報。 - [Cache::Memcached fix for problems with sockets used as string](http://lists.danga.com/pipermail/memcached/2005-April/001307.html) - [Nabble - memcached - Some issues with how last Cache::Memcached version (1.21) handle closing sockets/disconnets](http://www.nabble.com/Some-issues-with-how-last-Cache::Memcached-version-\(1.21\)-handle-closing-sockets-disconnets-t3932479.html)⁠(現在リンク切れ) ページの情報が消えてしまうとアレなので、備忘録として書き残しておきます。 sock_map って変数に socket のキャッシュが格納されるんですが、そのお掃除処理にバグありです。登録するときのキーは `$sock` なのに、お掃除するときのキーは `\$sock` になっています。これでは死んだ socket がいつまでも掃除されず、何度も使われてしまうという指摘です。 > And never attempt reconnect to memcached even if memcached full available... > > I looked into code and found some issues with how to %sock_map work: > > First inside function sock_to_host element of %sock_map hash initialize via: > > ```perl > $sock_map{$sock} = $host; > ``` > > From other hand we have 2 possible cleanup functions: > > ```perl > sub _dead_sock { > my ($sock, $ret, $dead_for) = @_; > if (my $ipport = $sock_map{\$sock}) { > my $now = time(); > $host_dead{$ipport} = $now + $dead_for > if $dead_for; > delete $cache_sock{$ipport}; > delete $sock_map{\$sock}; > } > @buck2sock = (); > return $ret; # 0 or undef, probably, depending on what caller wants > } > > sub _close_sock { > my ($sock) = @_; > if (my $ipport = $sock_map{\$sock}) { > close $sock; > delete $cache_sock{$ipport}; > delete $sock_map{\$sock}; > } > @buck2sock = (); > } > ``` > > Both of these function trying clean `$sock_map{\$sock}` ! > > So... Connect code set `$sock_map{$sock}` and cleanup code trying clean `$sock_map{\$sock}` !? > > And of course dead sock never cleaning at all and API try use dead sock again and again.... (because bad socket permanently cached in %sock_map) > > This is really painful issue leading instability of projects. > > PS: Cache::Memcached installed from fresh FreeBSD ports (and i compared with fresh cpan version... They are same) ## ひとまず解決? 本当に、これがサーバがフリーズした原因か??と、mysql の使用メモリの肥大化とか気になる点はいっぱいあるのですが、ひとまず解決?? ってか、このマシン不具合多すぎ。ある日突然おかしくなる。同じ version でも、何も変更していないのに動かなくなって、再インストールすると動くようになるとか、意味不明。同じ構成の別のマシンは快調なのになぁ・・・ まぁいいや。もうすぐリース切れで更改だから。