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

[6.0]Fix bug #5635 #5640

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions ext-src/swoole_pgsql.cc
Original file line number Diff line number Diff line change
@@ -23,30 +23,42 @@

#ifdef SW_USE_PGSQL

using swoole::Reactor;
using swoole::Coroutine;
using swoole::Reactor;
using swoole::coroutine::Socket;
using swoole::coroutine::translate_events_to_poll;

static SW_THREAD_LOCAL bool swoole_pgsql_blocking = true;

static int swoole_pgsql_socket_poll(PGconn *conn, swEventType event, double timeout = -1) {
static int swoole_pgsql_socket_poll(PGconn *conn, swEventType event, double timeout = -1, bool check_nonblock = false) {
if (swoole_pgsql_blocking) {
struct pollfd fds[1];
fds[0].fd = PQsocket(conn);
fds[0].events |= translate_events_to_poll(event);

int result = 0;
do {
result = poll(fds, 1, timeout);
result = poll(fds, 1, timeout);
} while (result < 0 && errno == EINTR);

return result > 0 ? 1 : errno == ETIMEDOUT ? 0 : -1;
}

Socket sock(PQsocket(conn), SW_SOCK_RAW);
sock.get_socket()->nonblock = 1;

bool retval = sock.poll(event, timeout);
while (check_nonblock && event == SW_EVENT_READ) {
if (PQconsumeInput(conn) == 0) {
retval = false;
break;
}
if (PQisBusy(conn) == 0) {
break;
}
retval = sock.poll(event, timeout);
}

sock.move_fd();
return retval ? 1 : sock.errCode == ETIMEDOUT ? 0 : -1;
}
@@ -68,7 +80,8 @@ static int swoole_pgsql_flush(PGconn *conn) {

static PGresult *swoole_pgsql_get_result(PGconn *conn) {
PGresult *result, *last_result = nullptr;
int poll_ret = swoole_pgsql_socket_poll(conn, SW_EVENT_READ);
// PQgetResult will block the process; it is necessary to forcibly check if the data is ready.
int poll_ret = swoole_pgsql_socket_poll(conn, SW_EVENT_READ, -1, true);
if (sw_unlikely(poll_ret == SW_ERR)) {
return nullptr;
}
71 changes: 71 additions & 0 deletions tests/swoole_pdo_pgsql/bug_5635.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
swoole_pdo_pgsql: Github bug #5635
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';
require __DIR__ . '/pdo_pgsql.inc';

use Swoole\Coroutine;
use Swoole\Coroutine\WaitGroup;
use Swoole\Coroutine\Channel;
use function Swoole\Coroutine\run;

ini_set("memory_limit", "-1");

$pdo = pdo_pgsql_test_inc::create();
$pdo->exec('create table bug_5635 (id int, data varchar(1024));');
$pdo->exec(<<<EOL
DO $$
BEGIN
FOR i IN 1..5000000 LOOP
INSERT INTO bug_5635(id, data) VALUES (i, 'data' || i);
END LOOP;
END $$;
EOL);

Coroutine::set(['hook_flags' => SWOOLE_HOOK_PDO_PGSQL]);
run(function() {
$waitGroup = new WaitGroup();
$channel = new Channel(1);

Coroutine::create(function() use ($waitGroup, $channel) {
$start = time();
$waitGroup->add();
$pdo = pdo_pgsql_test_inc::create();
$stmt = $pdo->query("select * from bug_5635;");
$data = $stmt->fetchAll();
Assert::true(count($data) == 5000000);
$channel->push($data ?? [], 10);
$waitGroup->done();
echo 'DONE' . PHP_EOL;
});

Coroutine::create(function() use ($waitGroup, $channel) {
$waitGroup->add();
$result = $channel->pop(1.5);
if (!$result) {
echo 'channel pop timeout' . PHP_EOL;
}
$waitGroup->done();
});

var_dump(1);
Coroutine::sleep(1);
var_dump(2);
$waitGroup->wait();
});
?>
--CLEAN--
<?php
require __DIR__ . '/../include/bootstrap.php';
require __DIR__ . '/pdo_pgsql.inc';
$pdo = pdo_pgsql_test_inc::create();
$pdo->exec('drop table bug_5635;');
?>
--EXPECTF--
int(1)
int(2)
channel pop timeout
DONE
Loading