Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 63BDF17BEC6 for ; Sun, 19 Feb 2012 18:32:25 -0400 (AST) Received: from mail-pz0-f46.google.com ([209.85.210.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RzFIu-0006y2-0o for pgsql-bugs@postgresql.org; Sun, 19 Feb 2012 22:32:24 +0000 Received: by daed14 with SMTP id d14so5152446dae.19 for ; Sun, 19 Feb 2012 14:32:11 -0800 (PST) Received-SPF: pass (google.com: domain of maumau307@gmail.com designates 10.68.225.231 as permitted sender) client-ip=10.68.225.231; Authentication-Results: mr.google.com; spf=pass (google.com: domain of maumau307@gmail.com designates 10.68.225.231 as permitted sender) smtp.mail=maumau307@gmail.com; dkim=pass header.i=maumau307@gmail.com Received: from mr.google.com ([10.68.225.231]) by 10.68.225.231 with SMTP id rn7mr36380871pbc.64.1329690731191 (num_hops = 1); Sun, 19 Feb 2012 14:32:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:from:to:subject:date:mime-version:content-type :x-priority:x-msmail-priority:x-mailer:x-mimeole; bh=+ZJo72XzEOdE6aQ7ZlVHfMc/nJco4vBOwbxld5rwjrw=; b=jrVxSsuRzHEcJh0axmFT3zqZRwDSqQuMyBV1x3QoQVdUDjqiJrORcGwRLB8XqeGoP9 1bj0uKoZm++7S/0chO5EzkAH+CkHFvRI1yYhdV9EmuuSrXLh6eDrKp6rGPNRWeGcoGt2 Uo6iwQfKxLsW2FwrD1yGirO4WQYdpJnek7DtM= Received: by 10.68.225.231 with SMTP id rn7mr30253866pbc.64.1329690731122; Sun, 19 Feb 2012 14:32:11 -0800 (PST) Received: from maumau (p1042-ipbfp401sizuokaden.shizuoka.ocn.ne.jp. [122.20.60.42]) by mx.google.com with ESMTPS id q10sm23004352pbb.10.2012.02.19.14.32.09 (version=SSLv3 cipher=OTHER); Sun, 19 Feb 2012 14:32:10 -0800 (PST) Message-ID: <9B64D90FC69E4432BBFA5D72D1844F64@maumau> From: "MauMau" To: Subject: [Bug fix] postmaster always crashes on a debug version of Windows Date: Mon, 20 Feb 2012 07:32:19 +0900 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_069F_01CCEFA1.C6CB7F20" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Windows Mail 6.0.6002.18197 X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6002.18463 X-Pg-Spam-Score: -2.3 (--) X-Archive-Number: 201202/142 X-Sequence-Number: 32485 This is a multi-part message in MIME format. ------=_NextPart_000_069F_01CCEFA1.C6CB7F20 Content-Type: text/plain; format=flowed; charset="iso-2022-jp"; reply-type=original Content-Transfer-Encoding: 7bit Hello Let me also post this here at pgsql-bugs just in case, though I post it pgsql-hackers on Feb 16. ---------- Hello I encountered a bug which always causes PostgreSQL to crash on Windows. Attached is a patch that fixes it. Please review it and include it in the upcoming minor releases of supported versions. The following is a bug report. Your name :MauMau Your email address :maumau307@gmail.com System Configuration: --------------------- Architecture (example: Intel Pentium) : Intel Xeon Operating System (example: Linux 2.4.18) : I found the bug on Windows 2008 R2 Enterprise x64 Edition, but it should apply to all versions of Windows. PostgreSQL version (example: PostgreSQL 9.1.1): PostgreSQL 8.3.12 However, the problem should happen on all versions of PostgreSQL. Compiler used (example: gcc 3.3.5) : Visual Studio 2005 Professional Edition Please enter a FULL description of your problem: ------------------------------------------------ On a debug version of Windows, PostgreSQL server fails to start. When I run "pg_ctl start" on the command prompt, pg_ctl displays "server starting", but the PostgreSQL server does not start. No message is left which tells the cause. pg_ctl does not show any error messages on the command prompt. Even when I include 'eventlog' in log_destination parameter, no error message is recorded in the application log nor the system log of Windows event log. This problem does not occur on a release version of Windows. Please describe a way to repeat the problem. Please try to provide a concise reproducible example, if at all possible: ---------------------------------------------------------------------- On a debug version of Windows, do the following: 1. Run initdb. initdb's option is not relevant. 2. Set logging_collector to on in postgresql.conf. 3. Run "pg_ctl start" on the command prompt. The problem does not happen when logging_collector is off. If you know how this problem might be fixed, list the solution below: --------------------------------------------------------------------- [Cause] postmaster crashes in the following CloseHandle() call, which is at line 591 in src/backend/postmaster/syslogger.c (in case of PostgreSQL 9.1.) /* Now we are done with the write end of the pipe. */ CloseHandle(syslogPipe[1]); The CloseHandle() crashes because it receives an already closed handle. According the following MSDN article, CloseHandle() causes a crash under a debugger. Though I'm not exactly sure, I guess running on the debug version of Windows is equivalent to running under a debugger. http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx [Excerpt] If the application is running under a debugger, the function will throw an exception if it receives either a handle value that is not valid or a pseudo-handle value. This can happen if you close a handle twice, or if you call CloseHandle on a handle returned by the FindFirstFile function instead of calling the FindClose function. Then, where is the handle closed? It was closed in close() at three lines above. The pipe handle is associated with a file descriptor by _open_osfhandle(). Calling close() for the file descriptor also closes the underlying handle. See the MSDN article: http://msdn.microsoft.com/en-us/library/bdts1c9x(v=vs.80).aspx [Excerpt] To close a file opened with _open_osfhandle, call _close. The underlying handle is also closed by a call to _close, so it is not necessary to call the Win32 function CloseHandle on the original handle. [Fix] Remove the unnecessary CloseHandle() call. I sought similar extra closing by checking places where _open_osfhandle() or _get_osfhandle() is used, but there was nothing elsewhere. I attached a patch against the master HEAD. Is it OK with a patch that has CR-LF line endings? Regards ------=_NextPart_000_069F_01CCEFA1.C6CB7F20 Content-Type: application/octet-stream; name="postmaster_crash_on_debug_windows.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="postmaster_crash_on_debug_windows.patch" diff --git a/src/backend/postmaster/syslogger.c = b/src/backend/postmaster/syslogger.c new file mode 100644 index cea896a..c331dd9 *** a/src/backend/postmaster/syslogger.c --- b/src/backend/postmaster/syslogger.c *************** SysLogger_Start(void)=0D *** 588,595 **** errmsg("could not redirect stderr: %m")));=0D close(fd);=0D _setmode(_fileno(stderr), _O_BINARY);=0D ! /* Now we are done with the write end of the pipe. */=0D ! CloseHandle(syslogPipe[1]);=0D syslogPipe[1] =3D 0;=0D #endif=0D redirection_done =3D true;=0D --- 588,598 ---- errmsg("could not redirect stderr: %m")));=0D close(fd);=0D _setmode(_fileno(stderr), _O_BINARY);=0D ! /*=0D ! * Now we are done with the write end of the pipe.=0D ! * CloseHandle() must not be called because the preceding=0D ! * close() closes the underlying handle.=0D ! */=0D syslogPipe[1] =3D 0;=0D #endif=0D redirection_done =3D true;=0D ------=_NextPart_000_069F_01CCEFA1.C6CB7F20--