[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

A solution for starting background jobs on Windows from post-commit hooks

From: Christian <christian_at_atombrenner.de>
Date: Wed, 12 Nov 2008 15:39:47 +0100

"A solution for how to start a background job from a subversion
post-commit hook on windows. See issues 2619, 2560 and 2497"

There are some issues (2619, 2560, 2497) regarding starting background
jobs from post-commit hooks on windows. These issues are minor ones on
Unix, all you have to do to is to redirect standard output and error.

On Windows this workaround doesn't work. It is not possible to start
background processes with the Windows "start" command. I will later
explain why and how to get the desired behavior.

Because when I first run into this issue it hits me completely
unexpected. I wanted to update a fulltext index of the repository on
every commit and doing this in the background was an absolute must.
It took me a lot of time to figure out what was going on. All I could
find were some crazy workarounds like using the scheduler (at) to start
the real job a minute later or installing cygwin to get simulate a linux
like environment. I even found a commercial product, the "Subversion
Hook Accelerator" http://www.clearvision-cm.com/products/3945.html that
solves this issue.

Because I (Google) didn't find some good answer but instead
found a lot of people having the same problem I like to
share my findings here on the user mailing list.

Problem Analysis (Windows):
The subversion process waits for the post-commit hook process to finish.
It does so by creating some pipes for redirecting stdin and stderr
(Handles), creating a process that *inherits* those handles and waiting
for all handles being closed. Unfortunately, a windows a child process
inherits all handles (e.g. pipes) from its parent process if it is not
created with some special flags (DETACHED_PROCESS) and bInheritHandles
set to FALSE. Even more unfortunately the windows "start" command
doesn't set this flags and therefore it is not possible to create a
background job with "start" from a subversion hook script.

So this is not a subversion issue. It is simply that "start" is the
wrong tool to start a background job from a subversion hook.
You need to start a "detached" windows process and this is what the
so called "Subversion Hook Accelerator" probably does.

Here comes the C++ source of a simple "RunDetached" program that can
be used to start a real, detached background process from any
subversion hook script. Build the RunDetached.exe and use it like this:

RunDetached YourPath\YourProgram.exe YourFirstArg YourSecondArg

-------------------------Snippet-------------------------------

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

int _tmain()
{
    LPWSTR pCmd = ::GetCommandLine();

    // skip the executable
    if (*pCmd++ == L'"') while (*pCmd++ != L'"');
    else while (*pCmd != NULL && *pCmd != L' ') ++pCmd;
    while (*pCmd++ == L' ');

    STARTUPINFO si;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);

    PROCESS_INFORMATION pi;
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    BOOL result = CreateProcess
    (
        NULL, // No module name (use command line)
        pCmd, // Command line
        NULL, // Process handle not inheritable
        NULL, // Thread handle not inheritable
        FALSE, // Set bInheritHandles to FALSE
        DETACHED_PROCESS, // Detach process
        NULL, // Use parent's environment block
        NULL, // Use parent's starting directory
        &si, // Pointer to STARTUPINFO structure
        &pi // Pointer to PROCESS_INFORMATION structure
(returned)
    );

    if (result) return 0;

    wchar_t msg[2048];
    FormatMessage
    (
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        ::GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
        msg, sizeof(msg),
        NULL
    );
    fputws(msg, stderr);
    _flushall();

    return -1;
}

-------------------------Snippet-------------------------------

Hope that helps some windows users here.

---
Christian Rodemeyer
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_subversion.tigris.org
For additional commands, e-mail: users-help_at_subversion.tigris.org
Received on 2008-11-12 15:39:30 CET

This is an archived mail posted to the Subversion Users mailing list.