HEX
Server: Apache
System: Windows NT MAGNETO-ARM 10.0 build 22000 (Windows 10) AMD64
User: Michel (0)
PHP: 7.4.7
Disabled: NONE
Upload Files
File: C:/Ruby27-x64/share/doc/ruby/html/signals_rdoc.html
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>signals - RDoc Documentation</title>

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
</script>

<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">




<body id="top" role="document" class="file">
<nav role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2>
    <a href="./index.html" rel="home">Home</a>
  </h2>

  <div id="table-of-contents-navigation">
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#label-Caveats+for+implementing+Signal.trap+callbacks">Caveats for implementing Signal.trap callbacks</a>
    <li><a href="#label-An+implementation+detail+of+the+Ruby+VM">An implementation detail of the Ruby VM</a>
    <li><a href="#label-Unsafe+methods+to+call+in+Signal.trap+blocks">Unsafe methods to call in Signal.trap blocks</a>
    <li><a href="#label-Commonly+safe+operations+inside+Signal.trap+blocks">Commonly safe operations inside Signal.trap blocks</a>
    <li><a href="#label-System+call+wrapper+methods+which+are+safe+inside+Signal.trap">System call wrapper methods which are safe inside Signal.trap</a>
  </ul>
</div>


  <div id="project-metadata">
    <div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
  
    <li><a href="./CONTRIBUTING_md.html">CONTRIBUTING</a>
  
    <li><a href="./COPYING.html">COPYING</a>
  
    <li><a href="./COPYING_ja.html">COPYING.ja</a>
  
    <li><a href="./LEGAL.html">LEGAL</a>
  
    <li><a href="./NEWS.html">NEWS</a>
  
    <li><a href="./NEWS-1_8_7.html">NEWS-1.8.7</a>
  
    <li><a href="./NEWS-1_9_1.html">NEWS-1.9.1</a>
  
    <li><a href="./NEWS-1_9_2.html">NEWS-1.9.2</a>
  
    <li><a href="./NEWS-1_9_3.html">NEWS-1.9.3</a>
  
    <li><a href="./NEWS-2_0_0.html">NEWS-2.0.0</a>
  
    <li><a href="./NEWS-2_1_0.html">NEWS-2.1.0</a>
  
    <li><a href="./NEWS-2_2_0.html">NEWS-2.2.0</a>
  
    <li><a href="./NEWS-2_3_0.html">NEWS-2.3.0</a>
  
    <li><a href="./NEWS-2_4_0.html">NEWS-2.4.0</a>
  
    <li><a href="./NEWS-2_5_0.html">NEWS-2.5.0</a>
  
    <li><a href="./NEWS-2_6_0.html">NEWS-2.6.0</a>
  
    <li><a href="./README_ja_md.html">README.ja</a>
  
    <li><a href="./README_md.html">README</a>
  
    <li><a href="./bug_triaging_rdoc.html">bug_triaging</a>
  
    <li><a href="./contributing_rdoc.html">contributing</a>
  
    <li><a href="./contributors_rdoc.html">contributors</a>
  
    <li><a href="./dtrace_probes_rdoc.html">dtrace_probes</a>
  
    <li><a href="./extension_ja_rdoc.html">extension.ja</a>
  
    <li><a href="./extension_rdoc.html">extension</a>
  
    <li><a href="./globals_rdoc.html">globals</a>
  
    <li><a href="./keywords_rdoc.html">keywords</a>
  
    <li><a href="./lib/racc/pre-setup.html">pre-setup</a>
  
    <li><a href="./lib/racc/rdoc/grammar_en_rdoc.html">grammar.en</a>
  
    <li><a href="./maintainers_rdoc.html">maintainers</a>
  
    <li><a href="./marshal_rdoc.html">marshal</a>
  
    <li><a href="./regexp_rdoc.html">regexp</a>
  
    <li><a href="./security_rdoc.html">security</a>
  
    <li><a href="./signals_rdoc.html">signals</a>
  
    <li><a href="./standard_library_rdoc.html">standard_library</a>
  
    <li><a href="./syntax_rdoc.html">syntax</a>
  
    <li><a href="./syntax/assignment_rdoc.html">assignment</a>
  
    <li><a href="./syntax/calling_methods_rdoc.html">calling_methods</a>
  
    <li><a href="./syntax/comments_rdoc.html">comments</a>
  
    <li><a href="./syntax/control_expressions_rdoc.html">control_expressions</a>
  
    <li><a href="./syntax/exceptions_rdoc.html">exceptions</a>
  
    <li><a href="./syntax/literals_rdoc.html">literals</a>
  
    <li><a href="./syntax/methods_rdoc.html">methods</a>
  
    <li><a href="./syntax/miscellaneous_rdoc.html">miscellaneous</a>
  
    <li><a href="./syntax/modules_and_classes_rdoc.html">modules_and_classes</a>
  
    <li><a href="./syntax/precedence_rdoc.html">precedence</a>
  
    <li><a href="./syntax/refinements_rdoc.html">refinements</a>
  
    <li><a href="./win32/README_win32.html">README.win32</a>
  
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-label="Page signals.rdoc">

<h1 id="label-Caveats+for+implementing+Signal.trap+callbacks">Caveats for implementing <a href="Signal.html#method-c-trap"><code>Signal.trap</code></a> callbacks<span><a href="#label-Caveats+for+implementing+Signal.trap+callbacks">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>As with implementing signal handlers in C or most other languages, all code passed to <a href="Signal.html#method-c-trap"><code>Signal.trap</code></a> must be reentrant.  If you are not familiar with reentrancy, you need to read up on it at <a href="https://en.wikipedia.org/wiki/Reentrancy_(computing)">Wikipedia</a> or elsewhere before reading the rest of this document.</p>

<p>Most importantly, “thread-safety” does not guarantee reentrancy; and methods such as <a href="Mutex.html#method-i-lock"><code>Mutex#lock</code></a> and <a href="Mutex.html#method-i-synchronize"><code>Mutex#synchronize</code></a> which are commonly used for thread-safety even prevent reentrancy.</p>

<h2 id="label-An+implementation+detail+of+the+Ruby+VM">An implementation detail of the Ruby VM<span><a href="#label-An+implementation+detail+of+the+Ruby+VM">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The Ruby VM defers <a href="Signal.html#method-c-trap"><code>Signal.trap</code></a> callbacks from running until it is safe for its internal data structures, but it does not know when it is safe for data structures in YOUR code.  Ruby implements deferred signal handling by registering short C functions with only <a href="http://man7.org/linux/man-pages/man7/signal-safety.7.html">async-signal-safe functions</a> as signal handlers.  These short C functions only do enough tell the VM to run callbacks registered via <a href="Signal.html#method-c-trap"><code>Signal.trap</code></a> later in the main Ruby <a href="Thread.html"><code>Thread</code></a>.</p>

<h2 id="label-Unsafe+methods+to+call+in+Signal.trap+blocks">Unsafe methods to call in <a href="Signal.html#method-c-trap"><code>Signal.trap</code></a> blocks<span><a href="#label-Unsafe+methods+to+call+in+Signal.trap+blocks">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>When in doubt, consider anything not listed as safe below as being unsafe.</p>
<ul><li>
<p><a href="Mutex.html#method-i-lock"><code>Mutex#lock</code></a>, <a href="Mutex.html#method-i-synchronize"><code>Mutex#synchronize</code></a> and any code using them are explicitly unsafe.  This includes <a href="Monitor.html"><code>Monitor</code></a> in the standard library which uses <a href="Mutex.html"><code>Mutex</code></a> to provide reentrancy.</p>
</li><li>
<p><a href="Dir.html#method-c-chdir"><code>Dir.chdir</code></a> with block</p>
</li><li>
<p>any <a href="IO.html"><code>IO</code></a> write operations when <a href="IO.html#method-i-sync"><code>IO#sync</code></a> is false; including <a href="IO.html#method-i-write"><code>IO#write</code></a>, <a href="IO.html#method-i-write_nonblock"><code>IO#write_nonblock</code></a>, <a href="IO.html#method-i-puts"><code>IO#puts</code></a>. Pipes and sockets default to `IO#sync = true&#39;, so it is safe to write to them unless <a href="IO.html#method-i-sync"><code>IO#sync</code></a> was disabled.</p>
</li><li>
<p><a href="File.html#method-i-flock"><code>File#flock</code></a>, as the underlying flock(2) call is not specified by POSIX</p>
</li></ul>

<h2 id="label-Commonly+safe+operations+inside+Signal.trap+blocks">Commonly safe operations inside <a href="Signal.html#method-c-trap"><code>Signal.trap</code></a> blocks<span><a href="#label-Commonly+safe+operations+inside+Signal.trap+blocks">&para;</a> <a href="#top">&uarr;</a></span></h2>
<ul><li>
<p>Assignment and retrieval of local, instance, and class variables</p>
</li><li>
<p>Most object allocations and initializations of common types including <a href="Array.html"><code>Array</code></a>, <a href="Hash.html"><code>Hash</code></a>, <a href="String.html"><code>String</code></a>, <a href="Struct.html"><code>Struct</code></a>, <a href="Time.html"><code>Time</code></a>.</p>
</li><li>
<p>Common <a href="Array.html"><code>Array</code></a>, <a href="Hash.html"><code>Hash</code></a>, <a href="String.html"><code>String</code></a>, <a href="Struct.html"><code>Struct</code></a> operations which do not execute a block are generally safe; but beware if iteration is occurring elsewhere.</p>
</li><li>
<p><a href="Hash.html#method-i-5B-5D"><code>Hash#[]</code></a>, <a href="Hash.html#method-i-5B-5D-3D"><code>Hash#[]=</code></a> (unless <a href="Hash.html#method-c-new"><code>Hash.new</code></a> was given an unsafe block)</p>
</li><li>
<p>Thread::Queue#push and Thread::SizedQueue#push (since Ruby 2.1)</p>
</li><li>
<p>Creating a new <a href="Thread.html"><code>Thread</code></a> via <a href="Thread.html#method-c-new"><code>Thread.new</code></a>/Thread.start can used to get around the unusability of Mutexes inside a signal handler</p>
</li><li>
<p><a href="Signal.html#method-c-trap"><code>Signal.trap</code></a> is safe to use inside blocks passed to <a href="Signal.html#method-c-trap"><code>Signal.trap</code></a></p>
</li><li>
<p>arithmetic on <a href="Integer.html"><code>Integer</code></a> and <a href="Float.html"><code>Float</code></a> (`+&#39;, `-&#39;, &#39;%&#39;, &#39;*&#39;, &#39;/&#39;)</p>

<p>Additionally, signal handlers do not run between two successive local variable accesses, so shortcuts such as `+=&#39; and `-=&#39; will not trigger a data race when used on <a href="Integer.html"><code>Integer</code></a> and <a href="Float.html"><code>Float</code></a> classes in signal handlers.</p>
</li></ul>

<h2 id="label-System+call+wrapper+methods+which+are+safe+inside+Signal.trap">System call wrapper methods which are safe inside <a href="Signal.html#method-c-trap"><code>Signal.trap</code></a><span><a href="#label-System+call+wrapper+methods+which+are+safe+inside+Signal.trap">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Since Ruby has wrappers around many <a href="http://man7.org/linux/man-pages/man7/signal-safety.7.html">async-signal-safe C functions</a> the corresponding wrappers for many <a href="IO.html"><code>IO</code></a>, <a href="File.html"><code>File</code></a>, <a href="Dir.html"><code>Dir</code></a>, and <a href="Socket.html"><code>Socket</code></a> methods are safe.</p>

<p>(Incomplete list)</p>
<ul><li>
<p><a href="Dir.html#method-c-chdir"><code>Dir.chdir</code></a> (without block arg)</p>
</li><li>
<p><a href="Dir.html#method-c-mkdir"><code>Dir.mkdir</code></a></p>
</li><li>
<p><a href="Dir.html#method-c-open"><code>Dir.open</code></a></p>
</li><li>
<p><a href="File.html#method-i-truncate"><code>File#truncate</code></a></p>
</li><li>
<p><a href="File.html#method-c-link"><code>File.link</code></a></p>
</li><li>
<p><a href="File.html#method-c-open"><code>File.open</code></a></p>
</li><li>
<p><a href="File.html#method-c-readlink"><code>File.readlink</code></a></p>
</li><li>
<p><a href="File.html#method-c-rename"><code>File.rename</code></a></p>
</li><li>
<p><a href="File.html#method-c-stat"><code>File.stat</code></a></p>
</li><li>
<p><a href="File.html#method-c-symlink"><code>File.symlink</code></a></p>
</li><li>
<p><a href="File.html#method-c-truncate"><code>File.truncate</code></a></p>
</li><li>
<p><a href="File.html#method-c-unlink"><code>File.unlink</code></a></p>
</li><li>
<p><a href="File.html#method-c-utime"><code>File.utime</code></a></p>
</li><li>
<p><a href="IO.html#method-i-close"><code>IO#close</code></a></p>
</li><li>
<p><a href="Object.html#method-i-dup"><code>IO#dup</code></a></p>
</li><li>
<p><a href="IO.html#method-i-fsync"><code>IO#fsync</code></a></p>
</li><li>
<p><a href="IO.html#method-i-read"><code>IO#read</code></a></p>
</li><li>
<p><a href="IO.html#method-i-read_nonblock"><code>IO#read_nonblock</code></a></p>
</li><li>
<p><a href="IO.html#method-i-stat"><code>IO#stat</code></a></p>
</li><li>
<p><a href="IO.html#method-i-sysread"><code>IO#sysread</code></a></p>
</li><li>
<p><a href="IO.html#method-i-syswrite"><code>IO#syswrite</code></a></p>
</li><li>
<p><a href="IO.html#method-c-select"><code>IO.select</code></a></p>
</li><li>
<p><a href="IO.html#method-c-pipe"><code>IO.pipe</code></a></p>
</li><li>
<p><a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
</li><li>
<p><a href="Process.html#method-c-exit-21"><code>Process.exit!</code></a></p>
</li><li>
<p><a href="Process.html#method-c-fork"><code>Process.fork</code></a></p>
</li><li>
<p><a href="Process.html#method-c-kill"><code>Process.kill</code></a></p>
</li><li>
<p><a href="Process.html#method-c-pid"><code>Process.pid</code></a></p>
</li><li>
<p><a href="Process.html#method-c-ppid"><code>Process.ppid</code></a></p>
</li><li>
<p><a href="Process.html#method-c-waitpid"><code>Process.waitpid</code></a></p>
</li></ul>

<p>…</p>

</main>



<footer id="validator-badges" role="contentinfo">
  <p><a href="https://validator.w3.org/check/referer">Validate</a>
  <p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>