Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server script starts many times when using Swoole 6 #5649

Closed
nguyenhothanhtam0709 opened this issue Dec 31, 2024 · 2 comments
Closed

Server script starts many times when using Swoole 6 #5649

nguyenhothanhtam0709 opened this issue Dec 31, 2024 · 2 comments

Comments

@nguyenhothanhtam0709
Copy link

nguyenhothanhtam0709 commented Dec 31, 2024

Issue:
I ran a simple http server script using swoole 6 and SWOOLE_THREAD mode, the prinft show many times in console but it only shows 1 time in SWOOLE_PROCESS mode. It seems like this script run 1 time for each worker thread. Is this an expected behaviour or not?

1. What did you do? If possible, provide a simple script for reproducing the error.

I ran this script with Swoole 6

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Swoole\Constant;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use Workerman\Events\Swoole;

$server = new Server('0.0.0.0', 3001, SWOOLE_THREAD);
$server->set([
    'hook_flags' => SWOOLE_HOOK_ALL,
    'enable_coroutine' => true,
    'task_enable_coroutine' => true,
    'debug_mode' => true,
    'display_errors' => true,
    'enable_preemptive_scheduler' => true,
    'worker_num' => 4,
    'reactor_num' => 2,
    'task_worker_num' => 2
]);

$server->on(Constant::EVENT_WORKER_START, function () {
    $pid = getmypid();
    printf("Worker start on $pid\n");
});

$server->on('Task', function ($server, $task) {});

$server->on('request', function (Request $request, Response $response) {
    $response->end("OK");
});

$pid = getmypid();
printf("[thread] Server started at http://0.0.0.0:$server->port on process {$pid}\n");
$server->start();

2. What did you expect to see?

Line _[thread] Server started at _ shows only 1 time.

3. What did you see instead?

Line _[thread] Server started at _ shows only 8 times.

4. What version of Swoole are you using (show your php --ri swoole)?

swoole

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 6.0.0
Built => Dec 25 2024 10:41:50
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
thread => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 3.0.2 15 Mar 2022
dtls => enabled
http2 => enabled
json => enabled
pcre => enabled
zlib => 1.2.11
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled

Directive => Local Value => Master Value
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

5. What is your machine environment used (show your uname -a & php -v & gcc -v) ?

uname -a

Linux PF4GRXXV 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov  6 17:42:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

php -v

PHP 8.4.2 (cli) (built: Dec 25 2024 09:55:31) (ZTS)
Copyright (c) The PHP Group
Zend Engine v4.4.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.2, Copyright (c), by Zend Technologies

gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
@matyhtf
Copy link
Member

matyhtf commented Jan 2, 2025

This is as expected. Unlike processes, multithreading in PHP operates differently because of the thorough isolation enforced by PHP's ZTS (Zend Thread Safety). As a result, child threads cannot inherit any resources from the parent thread, meaning each worker thread runs in a brand new VM environment.

The code segment containing new Server in a child thread will be re-executed within its own context.

There are two key configurations related to SWOOLE_THREAD:

  • The bootstrap option allows you to set an entry file for worker threads; by default, it uses the index.php file specified when running php-cli.
  • The init_arguments option enables the creation of resources that can be passed as parameters to worker threads. These thread parameters can be accessed within worker threads using the Thread::getArguments() method.

@nguyenhothanhtam0709
Copy link
Author

@matyhtf , thank you for your explanation.

@matyhtf matyhtf closed this as completed Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants