<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[118303] trunk/base/src/registry2.0/portimage.tcl</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="https://trac.macports.org/changeset/118303">118303</a></dd>
<dt>Author</dt> <dd>cal@macports.org</dd>
<dt>Date</dt> <dd>2014-03-29 12:20:00 -0700 (Sat, 29 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>base: prevent further race conditions with SIGINT and SIGTERM during activation and deactivation where possible</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbasesrcregistry20portimagetcl">trunk/base/src/registry2.0/portimage.tcl</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbasesrcregistry20portimagetcl"></a>
<div class="modfile"><h4>Modified: trunk/base/src/registry2.0/portimage.tcl (118302 => 118303)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/base/src/registry2.0/portimage.tcl        2014-03-29 16:41:17 UTC (rev 118302)
+++ trunk/base/src/registry2.0/portimage.tcl        2014-03-29 19:20:00 UTC (rev 118303)
</span><span class="lines">@@ -134,7 +134,6 @@
</span><span class="cx">     ui_msg &quot;$UI_PREFIX [format [msgcat::mc &quot;Activating %s @%s&quot;] $name $specifier]&quot;
</span><span class="cx"> 
</span><span class="cx">     _activate_contents $requested
</span><del>-    $requested state installed
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> # takes a composite version spec rather than separate version,revision,variants
</span><span class="lines">@@ -201,7 +200,6 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     _deactivate_contents $requested [$requested files] $force
</span><del>-    $requested state imaged
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> proc _check_registry {name version revision variants} {
</span><span class="lines">@@ -541,6 +539,11 @@
</span><span class="cx">                         lappend rollback_filelist $file
</span><span class="cx">                     }
</span><span class="cx">                 }
</span><ins>+
+                # Recording that the port has been activated should be done
+                # here so that this information cannot be inconsistent with the
+                # state of the files on disk.
+                $port state installed
</ins><span class="cx">             } catch {{POSIX SIG SIGINT} eCode eMessage} {
</span><span class="cx">                 # Pressing ^C will (often?) print &quot;^C&quot; to the terminal; send
</span><span class="cx">                 # a linebreak so our message appears after that.
</span><span class="lines">@@ -563,30 +566,40 @@
</span><span class="cx">         }
</span><span class="cx">     } catch {*} {
</span><span class="cx">         # This code must run to completion, or the installation might be left
</span><del>-        # in an inconsistent state
-        signal block {TERM INT}
</del><ins>+        # in an inconsistent state. We store the old signal handling state,
+        # block the critical signals and restore to the previous state instead
+        # of unblocking.
+        # Note that this still contains a race condition: A user could press ^C
+        # fast enough so that the second error arrives before the error is
+        # caught, re-thrown and re-caught here. As far as I can see, there's no
+        # easy way around this problem.
+        set osignals [signal get {TERM INT}]
+        try {
+            # Block signals to avoid inconsistiencies.
+            signal block {TERM INT}
</ins><span class="cx"> 
</span><del>-        # roll back activation of this port
-        if {[info exists deactivate_this]} {
-            _deactivate_contents $port $rollback_filelist yes yes
-        }
-        # if any errors occurred, move backed-up files back to their original
-        # locations, then rethrow the error. Transaction rollback will take care
-        # of this in the registry.
-        foreach file $backups {
-            ::file rename -force -- &quot;${file}${baksuffix}&quot; $file
-        }
-        # reactivate deactivated ports
-        foreach entry [array names todeactivate] {
-            if {[$entry state] eq &quot;imaged&quot; &amp;&amp; ($noexec || ![registry::run_target $entry activate &quot;&quot;])} {
-                activate [$entry name] [$entry version] [$entry revision] [$entry variants] [list ports_activate_no-exec $noexec]
</del><ins>+            # roll back activation of this port
+            if {[info exists deactivate_this]} {
+                _deactivate_contents $port $rollback_filelist yes yes
</ins><span class="cx">             }
</span><ins>+            # if any errors occurred, move backed-up files back to their original
+            # locations, then rethrow the error. Transaction rollback will take care
+            # of this in the registry.
+            foreach file $backups {
+                ::file rename -force -- &quot;${file}${baksuffix}&quot; $file
+            }
+            # reactivate deactivated ports
+            foreach entry [array names todeactivate] {
+                if {[$entry state] eq &quot;imaged&quot; &amp;&amp; ($noexec || ![registry::run_target $entry activate &quot;&quot;])} {
+                    activate [$entry name] [$entry version] [$entry revision] [$entry variants] [list ports_activate_no-exec $noexec]
+                }
+            }
+        } finally {
+            # We've completed all critical operations, re-enable the TERM and
+            # INT signals.
+            signal set $osignals
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        # We've completed all critical operations, re-enable the TERM and INT
-        # signals.
-        signal unblock {TERM INT}
-
</del><span class="cx">         # remove temp image dir
</span><span class="cx">         ::file delete -force $extracted_dir
</span><span class="cx">         throw
</span><span class="lines">@@ -660,18 +673,38 @@
</span><span class="cx">     # are after their elements.
</span><span class="cx">     set files [lsort -decreasing -unique $files]
</span><span class="cx"> 
</span><del>-    # Remove all elements.
-    if {!$rollback} {
-        registry::write {
-            $port deactivate $imagefiles
</del><ins>+    # Avoid interruptions while removing the files and updating the database to
+    # prevent inconsistencies from forming between filesystem and database.
+    set osignals [signal get {TERM INT}]
+
+    try {
+        # Block the TERM and INT signals to avoid being interrupted. Note that
+        # they might already be block at this point because
+        # _deactivate_contents might be called during rollback of
+        # _activate_contents, but because we're storing the old signal state
+        # and returning to that instead of unblocking it doesn't matter.
+        signal block {TERM INT}
+
+        # Remove all elements.
+        if {!$rollback} {
+            registry::write {
+                $port deactivate $imagefiles
+                foreach file $files {
+                    _deactivate_file $file
+                }
+
+                # Update the port's state in the same transaction as the file
+                # delete operations.
+                $port state imaged
+            }
+        } else {
</ins><span class="cx">             foreach file $files {
</span><span class="cx">                 _deactivate_file $file
</span><span class="cx">             }
</span><span class="cx">         }
</span><del>-    } else {
-        foreach file $files {
-            _deactivate_file $file
-        }
</del><ins>+    } finally {
+        # restore the signal block state
+        signal set $osignals
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>