Skip to content

Commit

Permalink
Test and fix memcache session redundancy php8 (#87)
Browse files Browse the repository at this point in the history
* Use zend_bool for ini bool settings

I think that these might have uninitialized bytes
when a data type larger than zend_bool is used for the ini setting,
if the module globals are set from malloc?
(Not 100% sure if the entire structure isn't set to 0 before being used)

Related to #56

* add a redundancy test file

add a redundancy test file checking multiple memcached restarts
install pcntl in docker container so tests are running
add also a Vagrantfile for easier dev env setup

* fix the redundancy failover by having dataresult as null initially instead of empty string

Co-authored-by: Tyson Andre <[email protected]>
Co-authored-by: Tomas Srnka <[email protected]>
  • Loading branch information
3 people authored May 28, 2021
1 parent e014963 commit c615b13
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
ARG PHP_IMAGE=php:8.0
FROM $PHP_IMAGE

RUN docker-php-ext-configure pcntl --enable-pcntl \
&& docker-php-ext-install -j$(nproc) pcntl

RUN apt-get update && apt-get install -y \
git \
zlib1g-dev \
memcached ;
memcached ;

COPY docker/host.conf /etc/host.conf

Expand Down
17 changes: 17 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = 'ubuntu/bionic64'

config.vm.provider :virtualbox do |vb|
vb.name = 'ext-memcache-dev'
vb.memory = 1024
vb.cpus = 2
end

config.vm.provision 'docker'

end
2 changes: 1 addition & 1 deletion src/memcache_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ PS_READ_FUNC(memcache)
ZVAL_NULL(&addresult);

/* third request fetches the data, data is only valid if either of the lock requests succeeded */
ZVAL_EMPTY_STRING(&dataresult);
ZVAL_NULL(&dataresult);

/* create requests */
if (php_mmc_session_read_request(pool, &zkey, lockparam, &addresult, dataparam, &lockrequest, &addrequest, &datarequest) != MMC_OK) {
Expand Down
75 changes: 75 additions & 0 deletions tests/redundancy_test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
--TEST--
redundancy test
--SKIPIF--
<?php include 'connect.inc'; if (!MEMCACHE_HAVE_SESSION) print 'skip not compiled with session support'; else if (!function_exists('pcntl_fork')) print 'skip not compiled with pcntl_fork() support'; ?>
--FILE--
<?php

include 'connect.inc';

$sid = md5(rand());

ini_set('session.save_handler', 'memcache');
ini_set('memcache.session_save_path', "tcp://$host:$port,tcp://$host2:$port2");
ini_set('memcache.session_redundancy', 3);

$memcache1 = test_connect1();
$memcache2 = test_connect2();

$pid = pcntl_fork();
if (!$pid) {
// In child process
session_id($sid);
session_start();
if (!isset($_SESSION['counter']))
$_SESSION['counter'] = 0;
$_SESSION['counter'] += 1;
session_write_close();

exit(0);
}
pcntl_waitpid($pid, $status);

$memcache1->flush();

$pid = pcntl_fork();
if (!$pid) {
// In child process
session_id($sid);
session_start();
if (!isset($_SESSION['counter']))
$_SESSION['counter'] = 0;
$_SESSION['counter'] += 1;
session_write_close();

exit(0);
}
pcntl_waitpid($pid, $status);

$memcache2->flush();

$pid = pcntl_fork();
if (!$pid) {
// In child process
session_id($sid);
session_start();
if (!isset($_SESSION['counter']))
$_SESSION['counter'] = 0;
$_SESSION['counter'] += 1;
session_write_close();

exit(0);
}
pcntl_waitpid($pid, $status);


session_id($sid);
session_start();
var_dump($_SESSION);

?>
--EXPECT--
array(1) {
["counter"]=>
int(3)
}

0 comments on commit c615b13

Please sign in to comment.