php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62001 socket don't close
Submitted: 2012-05-11 07:08 UTC Modified: 2012-05-12 17:08 UTC
From: dark9 at rune dot ws Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.4.3 OS: ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dark9 at rune dot ws
New email:
PHP Version: OS:

 

 [2012-05-11 07:08 UTC] dark9 at rune dot ws
Description:
------------
pleses exec test script.

Test script:
---------------
******************************test.php**************************
#! /usr/bin/php
<?php
$errno=0;
$errstr='';
$socket = stream_socket_server("tcp://0.0.0.0:4321", $errno, $errstr);
$while=__DIR__.DIRECTORY_SEPARATOR.'while.php > /dev/null &';
`$while`;
unset($socket);
echo `lsof -i:4321`;
******************************test.php end**********************



******************************while.php**************************
#! /usr/bin/php
<?php
while(true){}
******************************while.php end**********************

Expected result:
----------------
****************************** first exec ******************************
root@ubuntu:~# ./test.php

****************************** second exec ******************************
root@ubuntu:~# ./test.php

Actual result:
--------------
****************************** first exec ******************************
root@ubuntu:~# ./test.php
COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
while.php 29664 root    3u  IPv4 61634581      0t0  TCP *:4321 (LISTEN)

****************************** second exec ******************************
root@ubuntu:~# ./test.php
PHP Warning:  stream_socket_server(): unable to connect to tcp://0.0.0.0:4321 
(Address already in use) in /root/test.php on line 5

Warning: stream_socket_server(): unable to connect to tcp://0.0.0.0:4321 (Address 
already in use) in /root/test.php on line 5
COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
while.php 29664 root    3u  IPv4 61634581      0t0  TCP *:4321 (LISTEN)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-12 16:04 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2012-05-12 16:04 UTC] laruence@php.net
` ` is a fork,  so the socket fd you created will share to the child process, 
when you unset the socket, the refcount of it will decrement, but still not 
zero(child also ref to it). so~~

you can try:
<?php
$errno=0;
$errstr='';
$socket = stream_socket_server("tcp://0.0.0.0:4321", $errno, $errstr);
`
exec 3>&-
exec 4>&-
exec 5>&-
exec 6>&-
exec 7>&-
#you can add more
php /tmp/while.php >/dev/null &
`;
unset($socket);
echo `lsof -i:4321`;
 [2012-05-12 17:08 UTC] rasmus@php.net
Maybe we need a stream_setfd() call or add it to the stream context options to 
give people low-level access to set FD_CLOEXEC on arbitrary stream descriptors. 
That would make it a bit easier to control something like this, although it is a 
bit of an edge-case.
 [2012-05-14 05:36 UTC] dark9 at rune dot ws
thank for reply.

@laruence
if use 'exec' close fd then shell stream read/write etc have difficulty.

@rasmus
easier to control fd or new shell is good idea.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 07 06:01:29 2024 UTC