0

I am writing a REST plugin for Foswiki using Perl and I am facing an reliability issue when using File::Find. I have tried my best to write a minimal reproducible example. The plugin uses File::Find to traverse directories and print the filenames in the HTTP response. The REST request is working properly 4 times, but stop to work the 5th time. The HTTP status remain “HTTP/1.1 200 OK” but no file is reported by File::Find anymore.

The webserver is nginx and is configured to use FastCGI. It appear to run 4 working threads managed by foswiki-fcgi-pm:

> ps aux
www-data 16957  0.0  7.7  83412 78332 ?        Ss   16:52   0:00 foswiki-fcgi-pm
www-data 16960  0.0  7.5  83960 76740 ?        S    16:52   0:00 foswiki-fcgi
www-data 16961  0.0  7.6  84004 76828 ?        S    16:52   0:00 foswiki-fcgi
www-data 16962  0.0  7.6  83956 76844 ?        S    16:52   0:00 foswiki-fcgi
www-data 16963  0.0  7.5  83960 76740 ?        S    16:52   0:00 foswiki-fcgi

Firstly, the plugin initialization simply register the REST handler:

sub initPlugin {
    my ( $topic, $web, $user, $installWeb ) = @_;

    # check for Plugins.pm versions
    if ( $Foswiki::Plugins::VERSION < 2.3 ) {
        Foswiki::Func::writeWarning( 'Version mismatch between ',
            __PACKAGE__, ' and Plugins.pm' );
        return 0;
    }

    Foswiki::Func::registerRESTHandler(
        'restbug', \&RestBug,
        authenticate => 0,  # Set to 0 if handler should be useable by WikiGuest
        validate     => 0,  # Set to 0 to disable StrikeOne CSRF protection
        http_allow => 'GET,POST', # Set to 'GET,POST' to allow use HTTP GET and POST
        description => 'Debug'
    );
    
    # Plugin correctly initialized
    return 1;
}

Secondly, the REST handler is implemented as follow, printing all the files it can possibly find:

sub RestBug {
  my ($session, $subject, $verb, $response) = @_;

  my @Directories = ("/var/www/foswiki/tools");

  sub findfilestest
  {
    $response->print("FILE $_\n");
  }

  find({ wanted => \&findfilestest }, @Directories );
}  

When I test the REST service with a HTTP request, the first 4 times I get the following HTTP response, which seems quite satisfying:

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 22 Nov 2022 09:23:10 GMT
Content-Length: 541
Connection: keep-alive
Set-Cookie: SFOSWIKISID=385db599c5d66bb19591e1eef7f1a854; path=/; secure; HttpOnly

FILE .
FILE foswiki.freebsd.init-script
FILE bulk_copy.pl
FILE dependencies
FILE mod_perl_startup.pl
FILE geturl.pl
FILE extender.pl
FILE extension_installer
FILE configure
FILE lighttpd.pl
FILE foswiki.freebsd.etc-defaults
FILE save-pending-checkins
FILE babelify
FILE upgrade_emails.pl
FILE tick_foswiki.pl
FILE foswiki.defaults
FILE rewriteshebang.pl
FILE fix_file_permissions.sh
FILE foswiki.init-script
FILE convertTopicSettings.pl
FILE mailnotify
FILE html2tml.pl
FILE tml2html.pl
FILE systemd
FILE foswiki.service

The following attempts give this unexpected response:

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 22 Nov 2022 09:24:56 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: SFOSWIKISID=724b2c4b1ddfbebd25d0dc2a0f182142; path=/; secure; HttpOnly

Note that if I restart Foswiki with the command systemctl restart foswiki, the REST service work again 4 more times.

How to make this REST service work more than 4 times in a row?

Robert
  • 2,711
  • 7
  • 15

0 Answers0