Thursday, December 29, 2005

When DBD::Pg v1.43 crashes host process under Windows [solution]

As I had to port one mod_perl+Postgres application to Windows recently, I came accross quite nasty behavior on a part of DBD::Pg DBI driver. It would simply crash apache instances while executing DBI::->connect(). Turned out, not only did it knock apache out of the sky, but also any freestanding perl code that executed DBI::->connect(). The following turned up without much digging around - a bug report, outstanding for some time on ActiveState website (http://bugs.activestate.com/show_bug.cgi?id=41887).Actually, crash seemingly occurs somewhere inside libpq, supposedly in PQconnectdb() routine. That's why perhaps this bug report is not of atop priority. Moreover, it seems like it does not even matter whether postmaster is running or not.

Anyway, the following is a DBD::Pg re-building procedure, which worked for me:

Prerequisites
* MS Visual C/C++ (part of Misual Studio).NET 2003 Professional. Other (later) versions may also suffice, but I have not got them so it remains questionable. Also freely available cut down versions of Visual C++ (like Vusial Studio Express or Visual C/C++ .NET 2003 compiler) perhaps won't do due to windows API being ripped off.
* ActiveState Perl 5.8.7
* DBI 1.48
* Latest PostgreSQL sources (v8.1.1 as of writing), for example from http://www.postgresql.org/ftp/source/v8.1.1/
* DBD::Pg v1.43 sources (http://search.cpan.org/CPAN/authors/id/D/DB/DBDPG/DBD-Pg-1.43.tar.gz)

Building libpg
* Extract PostgreSQL sources to some directory, further referred to as PGSQL
* Open Visual Studio .NET 2003 command prompt and navigate to PGSQL\src directory
* run "nmake -f win32.mak" and wait until it finishes (without errors, hopefully)
* copy PGSQL\src\interfaces\libpq\libpq-fe.h to PGSQL\src\include

Building DBD::Pg
* Extract DBD::Pg v1.43 sources to some directiry, further referred to as DBDPG
* Set up two environment variables in Visual Studio .NET 2003 command prompt:

set POSTGRES_INCLUDE=PGSQL\src\include
set POSTGRES_LIB=PGSQL\src\interfaces\libpq\Release

* Change directory to DBDPG
* Open dbdimp.c file in some editor and add the following one (highlighted) line after line 29:
#ifdef WIN32
#define snprintf _snprintf
#define strcasecmp(x,y) _stricmp(x,y)
#endif
* Run "perl Makefile.PL". This time around it should not complain about missing -lpq...
* Run "nmake" and "nmake install"

That's it. You should have a working DBD::Pg at this point.

Thursday, October 06, 2005

Update: Bug in Apache::Session::Lock::Semaphore (v1.01)

Great news: just got a quick e-mail from the maintainer that the patch is incorporated into Apache::Session 1.80 and released to CPAN. Thanks Casey!

Tuesday, September 06, 2005

Bug in Apache::Session::Lock::Semaphore (v1.01)

There is a bug in Apache::Session::Lock::Semaphore (v1.01) that prevents it from working when Transaction mode is enabled. Apache::Session package's (which Apache::Session::Lock::Semaphore is a part of) maintainer (Casey West) has been contacted on numereous occasions with the patch, however with no avail. Thus, those who have experienced the bug, may use the following patch in the meanwhile (tested on Linux and OpenBSD):

=8<= Apache::Session::Lock::Semaphore 1.01 patch begin =8<=
--- Semaphore.pm 2005-05-07 12:38:30.000000000 -0400
+++ Semaphore.pm.org 2005-01-21 22:10:23.000000000 -0500
@@ -49,7 +49,7 @@
my $session = shift;

return if $self->{read};
- return if $self->{write};
+ die if $self->{write};

if (!$self->{sem}) {
$self->{sem} = new IPC::Semaphore($self->{sem_key},
$self->{nsems},
@@ -122,7 +122,7 @@

my $session = shift;

- return unless $self->{read};
+ die unless $self->{read};

$self->{sem}->o
=8<= Apache::Session::Lock::Semaphore 1.01 patch end =8<=