mojolicious / mojo-pg

Mojolicious :heart: PostgreSQL
https://metacpan.org/release/Mojo-Pg
Artistic License 2.0
101 stars 46 forks source link

PubSub disconnects very often under hypnotoad when EV is installed #69

Closed akarelas closed 3 years ago

akarelas commented 3 years ago

Steps to reproduce the behavior

As UNIX user postgres, create database my_db on localhost's postgresql, and allow user dbuser with password dbuser to connect to it, eg:

$ createdb my_db
$ createuser -P dbuser # enter `dbuser` as its password when asked

Then download and run this small mojolicious app:

$ git clone git@github.com:akarelas/demo-pubsub-ev-errors.git
$ cd demo-pubsub-ev-errors/
$ carton install
$ carton exec -- hypnotoad ./myapp.pl

Then watch the production log file:

$ tail -f log/production.log

Expected behavior

The log should not repeatedly mention disconnections from the Pg pubsub server.

Actual behavior

The log shows a warning message that Mojolicious disconnected from the pubsub server, many times per minute:

[2020-10-04 14:31:18.69635] [3930] [warn] Re-connected!!!
[2020-10-04 14:31:22.69282] [3929] [warn] Dis-connected!!!
[2020-10-04 14:31:22.69297] [3928] [warn] Dis-connected!!!
[2020-10-04 14:31:22.69329] [3930] [warn] Dis-connected!!!
[2020-10-04 14:31:23.69611] [3929] [warn] Re-connected!!!
[2020-10-04 14:31:23.69616] [3928] [warn] Re-connected!!!
[2020-10-04 14:31:23.69619] [3930] [warn] Re-connected!!!
[2020-10-04 14:31:27.69319] [3930] [warn] Dis-connected!!!
[2020-10-04 14:31:28.69561] [3930] [warn] Re-connected!!!
[2020-10-04 14:31:32.69258] [3929] [warn] Dis-connected!!!

This problem doesn't appear when morbo is running instead of hypnotoad.

akarelas commented 3 years ago

Pasting the current version of myapp.pl here, in case it gets modified in the future:

#!/usr/bin/env perl

use v5.32;
use warnings;
use FindBin '$RealBin';
use lib "$RealBin/local/lib/perl5";

use Mojolicious::Lite -signatures;

use Mojo::IOLoop;
use Mojo::Pg;

our ($pg, $pubsub);

Mojo::IOLoop->next_tick(sub {
  $pg = Mojo::Pg->new('postgresql://dbuser:dbuser@localhost/my_db');
  $pubsub = $pg->pubsub;
  $pubsub->listen(events => sub {});
  $pubsub->on(disconnect => sub {
    app->log->warn('Dis-connected!!!');
    warn 'Dis-connected';
  });
  $pubsub->on(reconnect => sub {
    app->log->warn('Re-connected!!!');
    warn 'Re-connected';
  });
  Mojo::IOLoop->recurring(5 => sub {
    $pubsub->notify(events => 'foo');
  });
});

get '/' => sub ($c) {
  $c->render(template => 'index');
};

app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Welcome to the Mojolicious real-time web framework!</h1>
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body><%= content %></body>
</html>
akarelas commented 3 years ago

I did a binary search for the commit of Mojo-Pg that started this problem, and found it is this one: d51e252b157bc05928d92a85f1b92be23626d590: "Handle notifications without eval (closes #58)"

akarelas commented 3 years ago

Reopened as #70