hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

socket file descriptor leak

Open apaloma opened this issue 12 years ago • 9 comments

Under some condition, HHVM will leak handles to sockets. After about a day of uptime, our production server stops serving requests. We get the occasional "Too many open files" request in the hhvm error log. Other requests just die. Looking at /proc/pid/fd shows thousands of lines for sockets.

$ sudo ls -l /proc/32169/fd | wc
  32766  360418 2481333

It's filled with sockets

lr-x------ 1 www-data www-data 64 Dec  3 22:56 0 -> /dev/null
l-wx------ 1 www-data www-data 64 Dec  3 22:56 1 -> /dev/null
lrwx------ 1 www-data www-data 64 Dec  3 22:56 10 -> socket:[3596582261]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 100 -> socket:[3597758510]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 1000 -> socket:[3605493219]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10000 -> socket:[3680948810]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10001 -> socket:[3678904959]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10002 -> socket:[3679435921]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10003 -> socket:[3681017174]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10004 -> socket:[3678952537]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10005 -> socket:[3678812944]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10006 -> socket:[3679018814]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10007 -> socket:[3681546683]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10008 -> socket:[3678877815]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10009 -> socket:[3678739319]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 1001 -> socket:[3604949673]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10010 -> socket:[3678055448]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10011 -> socket:[3677663437]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10012 -> socket:[3681492015]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10013 -> socket:[3678659403]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10014 -> socket:[3678429483]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10015 -> socket:[3677905669]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10016 -> socket:[3678179934]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10017 -> socket:[3683564723]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10018 -> socket:[3678138796]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10019 -> socket:[3678291679]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 1002 -> socket:[3608512492]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10020 -> socket:[3678042296]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10021 -> socket:[3678267390]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10022 -> socket:[3679512225]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10023 -> socket:[3678372093]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10024 -> socket:[3679972353]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10025 -> socket:[3679285769]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10026 -> socket:[3678047516]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10027 -> socket:[3678681504]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10028 -> socket:[3678422862]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10029 -> socket:[3678271286]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 1003 -> socket:[3605956097]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10030 -> socket:[3680417526]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10031 -> socket:[3679205947]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10032 -> socket:[3680241473]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10033 -> socket:[3678911797]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10034 -> socket:[3678134073]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10035 -> socket:[3678826980]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10036 -> socket:[3678192320]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10037 -> socket:[3678598995]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10038 -> socket:[3678412123]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 10039 -> socket:[3678885452]
lrwx------ 1 www-data www-data 64 Dec  4 15:15 1004 -> socket:[3606091696]
<snip>

After restarting HHVM, there are much fewer file descriptors open hovering at 300-400.

Sorry I don't have more information. Let me know if there's something I can do next time it happens to gather more information.

apaloma avatar Dec 04 '14 23:12 apaloma

We use pfsockopen on almost every page hit, so perhaps the bug is there somewhere.

apaloma avatar Dec 08 '14 19:12 apaloma

Interesting, are you marking these sockets as persistent? It looks like there is no mechanism to automatically close persistent sockets. Alternatively is it possible that you have a socket resource that doesn't get destructed before the end of the request (e.g. through cyclic references, as hhvm does not currently have a cycle collector)?

paulbiss avatar Dec 08 '14 21:12 paulbiss

They should be persistent, but we don't do anything special to mark the socket as persistent. My understanding was that this was done for us by using pfsockopen.

I had a theory that maybe if there was a socket error that the socket wouldn't be cleaned up, but I had a look at the code and it looks like HHVM does handle that: https://github.com/facebook/hhvm/blob/f80bfae3394cc48a0a99a49e003c7c3cd92e444f/hphp/runtime/ext/sockets/ext_sockets.cpp#L1330

Nothing that strange going on with the socket in our PHP code. We use pfsockopen with an IP, port, and timeout to get the handle. The only thing we do after that is fread, fwrite, stream_set_timeout, and potentially fclose if we hit an error case.

apaloma avatar Dec 09 '14 00:12 apaloma

We're also seeing this. We don't use persistent sockets anywhere. The only place in our code that uses sockets directly always closes the socket afterwards.

The only other place that we use sockets is indirectly through Redis. We don't close these connections, though the number of not properly closed connections is a lot smaller than the number of requests using Redis.

HRMsimon avatar Feb 01 '15 22:02 HRMsimon

To show the impact this has, this is a graph of the count of lsof -p $(pgrep hhvm) | wc -l over time. The drops are restarts, blue line is the ulimit.

hhvm_files-week

HRMsimon avatar Feb 02 '15 23:02 HRMsimon

Have upgrade to 3.5.1 and we're not seeing this any more.

HRMsimon avatar Feb 23 '15 23:02 HRMsimon

We had a rather nasty socket leak internally that was fixed by 0e12db8597d9271f3dde918a7d7abbf9a73f932f but that was in 3.5.0 so if the fix is new in 3.5.1 I'm not sure what did the trick, I don't see any potentially related cherry-picks.

paulbiss avatar Feb 23 '15 23:02 paulbiss

We were previously on 3.4.2, so could very well be that commit.

HRMsimon avatar Feb 23 '15 23:02 HRMsimon

Sounds like this is fixed, though I guess the mystery of how was never really resolved.

paulbiss avatar Sep 28 '16 23:09 paulbiss