Duplicate session id error in database table |
Hello!
We're using 3.1.5 with database sessions and we noticed that we would sometimes get the following error in the application logs: Quote:ERROR - 2017-07-22 15:32:19 --> Query error: Duplicate entry '[session_id]' for key 'PRIMARY' - Invalid query: INSERT INTO `db_session` (`id`, `ip_address`, `timestamp`, `data`) VALUES ('[session_id]', ...) The system is deployed on a Linode server with CloudFlare enabled. There also doesn't seem to be any recognizable pattern--it just happens randomly. This is from our config.php: PHP Code: $config['sess_driver'] = 'database'; And our MySQL database table: Code: CREATE TABLE IF NOT EXISTS `db_session` ( We found posts suggesting the ip_address should also be a primary key, but this doesn't apply to us since we have sess_match_ip set to FALSE. Any help would be appreciated. Thanks!
In CI 3.1.5 the id column must be varchar(128).
https://codeigniter.com/user_guide/libra...ase-driver
(07-22-2017, 08:27 AM)natanfelles Wrote: In CI 3.1.5 the id column must be varchar(128). Thanks natanfelles! We've updated the table: Code: mysql> describe db_session; We'll observe the logs and post an update if the issue is resolved or not.
Duplicate issue is still happening. We've had 2 new cases so far.
Code: ERROR - 2017-07-23 00:17:12 --> Query error: Duplicate entry 'aq64m773luqkia96p1h3lniaj1e4ngg6' for key 'PRIMARY' - Invalid query: INSERT INTO `db_session` (`id`, `ip_address`, `timestamp`, `data`) VALUES ('aq64m773luqkia96p1h3lniaj1e4ngg6', '162.158.138.38', 1500740232, '__ci_last_regenerate|i:1500739999;[other_data];')
Not sure, but it seems that this happens when the session id is regenerated.
Investigating how this process happens... https://github.com/bcit-ci/CodeIgniter/b...driver.php My question was whether there is any checking whether the new id already exists or not. Maybe the lock check is failing on your server.
Possible. We have 2 other sites on separate servers with the same set-up as this one, but with fewer users. There's just 1 case of a duplicate session id in one server with relatively fewer users, and 0 cases in the other server with the least number of users.
What can we possibly do to replicate this and/or prevent it from happening?
I'd check if expired sessions are being properly cleaned, especially if you're running Debian.
(07-24-2017, 12:21 AM)Narf Wrote: I'd check if expired sessions are being properly cleaned, especially if you're running Debian. Thanks, Narf. I checked the session table for distinct dates and it doesn't look like we have a problem with expired sessions. Good news though--we haven't had the duplicate session id issue for the last 3 days. I wanted to observe the site for a few days before I posted this update. So we added a call to session_write_close() in a function that generates PDF (via TCPDF) and serves it for download. Before we generate the PDF we set a specific variable using session->userdata so I noticed that practically all of the duplicate session id cases had that particular variable in the data part (as seen in application/logs). Then something about what natanfelles said regarding the lock check failing made me wonder if the locks were failing (?) for some users during the PDF generation, that's why we thought of just closing the session altogether right after we set the session variable and before TCPDF gets to work. We're still not sure if that's what was actually causing the issue and if closing the session was the right move, so we'd appreciate any insights on this. For now, we're just a bit relieved to not see the issue pop out in the logs for a couple of days. |
Welcome Guest, Not a member yet? Register Sign In |