I think I found out what's going on.
In the perl script, I'm "forking" a parallel process. The DB connection gets messed up as a consequence (sorry, I can't articulate better because I don't fully understand). But the answer (at least what worked for me), is to formally disconnect and then reconnect back in the main thread. Fortunately, I don't have any outstanding transactions at that time so I don't lose anything as far as that's concerned.
my $pid = fork()
if($pid == 0) {
<do whatever the forked proc needs to do>
}
$dbh->disconnect();
$dbh->connect(...)
proceed with the rest of the perl script.