File: extrefs_gc.pm

package info (click to toggle)
libpoe-perl 2%3A1.3670-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,996 kB
  • ctags: 1,416
  • sloc: perl: 22,865; makefile: 9
file content (88 lines) | stat: -rw-r--r-- 2,517 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# vim: ts=2 sw=2 expandtab

# Test a case that Yuval Kogman ran into.  Decrementing a reference
# count would immediately trigger a GC test.  During _start, that
# means a session might be GC'd before _start's handler returned.
# Fatal hilarity would ensue.

use warnings;
use strict;

use Test::More tests => 5;

sub POE::Kernel::ASSERT_DEFAULT () { 1 }
sub POE::Kernel::TRACE_DEFAULT  () { 0 }

use POE;

my $sigidle = 0;

# The "bystander" session is kept alive solely by its extra reference
# count.  It should be stopped when the "refcount" session destructs.
# This is determined by comparing the _stop time vs. SIGIDLE delivery.
# If _stop is first, then the bystander was reaped correctly.

my $bystander_id = POE::Session->create(
  inline_states => {
    _start => sub {
      $_[KERNEL]->refcount_increment( $_[SESSION]->ID, "just hold me");
    },
    _stop => sub {
      ok(
        !$sigidle,
        "bystander stopped before sigidle"
      );
    },
  },
)->ID;

# The "sigidle" session watches for SIGIDLE and sets a flag.  If the
# bystander is reaped after SIGIDLE, it means that the refcount
# session did not trigger its destruction.

POE::Session->create(
  inline_states => {
    _start => sub {
      $_[KERNEL]->sig( IDLE => 'got_sigidle' );
      $_[KERNEL]->alias_set("stayin_alive");
    },
    got_sigidle => sub {
      $sigidle++;
      pass("got sigidle");
    },
    _stop => sub {
      pass("sigidle session is allowed to stop");
    },
  },
);

# The "refcount" session attempts to trigger its own untimely
# destruction by incrementing and decrementing a reference count.  If
# it succeeds in killing itself off early, then its "do_something"
# event will cause a fatal runtime error when ASSERT_DEFAULT is on.
#
# As part of _stop, it decrements the extra reference on the bystander
# session, triggering its destruction before SIGIDLE.  If there's a
# problem, SIGIDLE will arrive first---because POE::Kernel has a
# refcount of 0 but the session still exists.

POE::Session->create(
  inline_states => {
    _start => sub {
      $_[KERNEL]->refcount_increment($_[SESSION]->ID, "just hold me");
      $_[KERNEL]->refcount_decrement($_[SESSION]->ID, "just hold me");
      $_[KERNEL]->yield("do_something");
    },
    do_something => sub {
      pass("refcount session is allowed to run");
    },
    _stop => sub {
      pass("refcount session is allowed to stop");
      $_[KERNEL]->refcount_decrement($bystander_id, "just hold me");
    },
  },
);

POE::Kernel->run();

1;