Date: | Thu, 31 Oct 2019 15:38:31 +0000 |
Subject: | Chrome data directory locked |
To: | "bug-WWW-Mechanize-Chrome [...] rt.cpan.org" <bug-WWW-Mechanize-Chrome [...] rt.cpan.org> |
From: | "Denley, Chris" <Chris.Denley [...] experian.com> |
I had an issue with WWW::Mechanize::Chrome not working if Chrome was already started. That seems to be resolved with using a different data directory than the already-running instance. I decided to use tempdir as demonstrated in the documentation for the data_directory option to ensure I always use a clean and unique data directory which is always deleted when the script finishes. However, there doesn't seem to be a good way for ensuring the Chrome process has completed and the temporary directory can be safely cleaned except for waiting a fixed period of time after the WWW::Mechanize::Chrome object is destroyed before allowing the tempdir variable to be destroyed, or waiting until CrashpadMetrics-active.pma can be deleted without error as in my workaround. Perhaps there should be a "close" method available that will return once all Chrome processes have completed and the data_directory can be safely deleted.
Chrome 78 on Windows 10
Strawberry Perl 5.24.4
WWW::Mechanize::Chrome 0.37
Without my workaround, this is the error I get when the temp directory cleanup fails:
cannot unlink file for C:\Users\do382cd\AppData\Local\Temp\1\HjecI5Ixs5\CrashpadMetrics-active.pma: Permission denied at C:/strawberry/perl/lib/File/Temp.pm line 784.
cannot remove directory for C:/Users/do382cd/AppData/Local/Temp/1/HjecI5Ixs5: Directory not empty at C:/strawberry/perl/lib/File/Temp.pm line 784.
Test script:
use strict;
use Log::Log4perl qw(:easy);
use WWW::Mechanize::Chrome;
use File::Temp 'tempdir';
Log::Log4perl->easy_init($ERROR); # Set priority of root logger to ERROR
my $tmpdir = tempdir(CLEANUP => 1 );
{
# limit scope of $mech so it is destroyed before $tmpdir
my $mech = WWW::Mechanize::Chrome->new(data_directory => $tmpdir);
$mech->get("https://metacpan.org/pod/WWW::Mechanize::Chrome");
}
exit(0); # comment this out for my workaround
# wait for file to be released so $tmpdir will be cleaned
my $path = "$tmpdir/CrashpadMetrics-active.pma";
for(1..10) {
last unless(-e $path);
unlink($path) and last;
sleep(1);
}
Message body is not shown because it is too large.