<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="289966" data-post-id="289966">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hst337" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hst337
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<pre data-code-wrap="elixir"><code class="lang-elixir">Mix.install [:gen_state_machine]

defmodule Server do
  use GenStateMachine, callback_mode: :state_functions

  def start_link(opts \\ []) do
    GenStateMachine.start_link(__MODULE__, opts)
  end

  def init(opts) do
    Process.flag(:trap_exit, true)
    {:ok, :state, opts}
  end

  def state(:info, message, data) do
    IO.inspect(message, label: :received)
    :keep_state_and_data
  end

  def terminate(reason, :state, data) do
    IO.inspect(reason, label: :terminating)
  end
end

DynamicSupervisor.start_link(name: Sup)
{:ok, child} = DynamicSupervisor.start_child(Sup, Server)
</code></pre>
<p>And in iex</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(3)&gt; Process.exit(child, :normal)
received: {:EXIT, #PID&lt;0.112.0&gt;, :normal}
true
iex(4)&gt; Process.exit(child, :kill)
true
iex(5)&gt; Process.info child
nil
</code></pre>
<p>As you can see, with <code>trap_exit</code>, normal exit results in just a message being sent. But if you call this</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(3)&gt; DynamicSupervisor.terminate_child Sup, child
terminating: :shutdown
:ok
</code></pre>
<p>It initiates the <code>terminate</code> callback</p>
<hr>
<p>P.S. don’t forget to mark the correct answer</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="289966" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/gracefully-shutdown-child-of-dynamic-supervisor/56183/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-289966" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="289966"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="291126" data-post-id="291126">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="_Seyed" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/_Seyed/120/25935_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  _Seyed
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hello <a class="mention" href="/u/lostkobrakai" rel="nofollow">@LostKobrakai</a> ,</p>
<p>Yep, I’ve placed the proper <code>handle_event</code> for managing exit messages and it should stop the process. I used this part of the documentation</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="291126" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/gracefully-shutdown-child-of-dynamic-supervisor/56183/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-291126" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="291126"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="291127" data-post-id="291127">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="_Seyed" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/_Seyed/120/25935_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  _Seyed
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi <a class="mention" href="/u/hst337" rel="nofollow">@hst337</a> ,<br>
Thanks a lot for the thorough explanation.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="291127" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/gracefully-shutdown-child-of-dynamic-supervisor/56183/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-291127" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="291127"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="291249" data-post-id="291249">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="_Seyed" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/_Seyed/120/25935_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  _Seyed
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hello again <a class="mention" href="/u/hst337" rel="nofollow">@hst337</a><br>
Thank you very much for your guidance. I’ve encountered an interesting case and it would be great if I could have your feedback.<br>
While reading the logs, it came to my attention that a child  was terminated 10 hours after <code>terminate_child/2</code> was issued. The child in question, is terminated from an external process other than the parent; it uses the <code>gen_statem</code> behavior and has two timeout events with a resolution of 10 minutes. In these timeout events, it opens an external file (therefore using an external resource).</p>
<p>Is termination postponed while the child is reading an external file when using <code>terminte_child/2</code>?<br>
Or if termination is issued during a timeout? 10 hours is a huuuge gap and I don’t understand what could be the problem.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="291249" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/gracefully-shutdown-child-of-dynamic-supervisor/56183/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-291249" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="291249"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="291251" data-post-id="291251">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="_Seyed" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/_Seyed/120/25935_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  _Seyed
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I edited the last paragraph.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="291251" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/gracefully-shutdown-child-of-dynamic-supervisor/56183/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-291251" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="291251"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="291252" data-post-id="291252">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hst337" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hst337
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>No, the termination is not postponed in any case. You need to take in account, that messages received before termination, are processed before <code>terminate</code> callback is called.</p>
<p>If you want to kill your child in an instant, you should delete child and kill with <code>:kill</code> reason. In this case, it won’t be possible to run terminate callback</p>
<p>Anyway, this is XY problem, since you’re trying to do resource management relying on <code>terminate</code> callback.</p>
<p>You need to know 2 things</p>
<ol>
<li>
<p><code>terminate</code> callback is for optimistic cleanup. This means that the callbacks is called only when exception can be handled. Some cases like hardware failure, out-of-memory errors and infinite loops are not covered by <code>terminate</code> callbacks. That’s why you should not rely on <code>terminate</code> callback</p>
</li>
<li>
<p>The idiomatic way to resource management is a resource pool or observer pattern. Latter is much more easier to implement, and it is basically a separate process, which hosts the resource and monitors the  process using the resource. In this case, when the user of the resource dies from any reason or stucks in infinite loop, your observing process will be able to close or cleanup the resource</p>
</li>
</ol> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="291252" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/gracefully-shutdown-child-of-dynamic-supervisor/56183/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-291252" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="291252"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="291351" data-post-id="291351">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="_Seyed" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/_Seyed/120/25935_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  _Seyed
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi <a class="mention" href="/u/hst337" rel="nofollow">@hst337</a> ,<br>
Thanks a lot for the feedback. I found the problem and it’s not related to<code> termiante_child/2</code>. Thank you very much for your insights.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="291351" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/gracefully-shutdown-child-of-dynamic-supervisor/56183/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-291351" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="291351"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>