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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="leonardorame" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  leonardorame
                    <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 created this Gist with the C++ side: <a href="https://gist.github.com/leonardorame/f8a932663ec642aae7cc4e1ca7062ba6" class="inline-onebox" rel="noopener nofollow ugc">ssr.cc · GitHub</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272092" 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/long-running-c-nif-segfaults/52602/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-272092" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272092"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="272095" data-post-id="272095">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jhogberg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/120/24176_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jhogberg
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks, having read the NIF and <code>DCMTK</code> source it’s very likely that this is a thread-safety issue. None of the <code>DCMTK</code> functions you use are documented as thread-safe and I saw too many thread-unsafe things to count while reading their source.</p>
<p>That the problem went away after commenting-out lines 27 through 38 also suggests that this is the case.</p>
<p>Try caching results instead of resources if width/height are mostly static, or if they’re very dynamic, create a new process for each instance and store those in the cache. When a new request comes in, ask the cached process to scale the image.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272095" 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/long-running-c-nif-segfaults/52602/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-272095" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272095"
                     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="272166" data-post-id="272166">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="leonardorame" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  leonardorame
                    <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>Thanks <a class="mention" href="/u/jhogberg" rel="nofollow">@jhogberg</a>, yes, one process per instance could be the answer. Now how can I start learning about that?, any pointer?.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272166" 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/long-running-c-nif-segfaults/52602/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-272166" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272166"
                     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="272176" data-post-id="272176">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jhogberg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/120/24176_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jhogberg
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You’re welcome <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>The gist of it would be:</p>
<ul>
<li>When there’s a cache miss:
<ol>
<li>Spawn a process that loads the file, then waits for requests to rescale the image (you might want to wait with a timeout to avoid having too many of these processes hanging around).</li>
<li>Store that process’ pid in the cache.</li>
<li>Ask said process to rescale the image.</li>
</ol>
</li>
<li>When there’s a cache hit:
<ol>
<li>Ask the cached process to rescale the image. If that fails (e.g. if we raced with the process dying due to timeout), treat it as a cache miss.</li>
</ol>
</li>
</ul>
<p>You’ll also want to fix all the memory leaks in your NIF module. At the very least, you need to free the <code>_dicomimage</code> after you’re done. This will be a lot easier if you fuse <code>createScaledImage</code>, <code>setMinMaxWindow</code> and <code>getPNG</code> into a single function.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272176" 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/long-running-c-nif-segfaults/52602/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-272176" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272176"
                     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="272404" data-post-id="272404">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="leonardorame" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  leonardorame
                    <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 again <a class="mention" href="/u/jhogberg" rel="nofollow">@jhogberg</a>, before spawning new processes I’m fixing memory leaks and trying to understand when the segmentation fault happens and found it crashes when the same cached resource is requested more than once at the same time.</p>
<p>In one of your answers you said <em>Try caching results instead of resources</em>, by this do you mean using <code>enif_make_binary</code> instead of <code>enif_make_resource</code>?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272404" 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/long-running-c-nif-segfaults/52602/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-272404" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272404"
                     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="272406" data-post-id="272406">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jhogberg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/120/24176_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jhogberg
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="leonardorame" data-post="16" data-topic="52602">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/l/f6c823/48.png" class="avatar"> leonardorame:</div>
<blockquote>
<p>Hi again <a class="mention" href="/u/jhogberg" rel="nofollow">@jhogberg</a>, before spawning new processes I’m fixing memory leaks and trying to understand when the segmentation fault happens and found it crashes when the same cached resource is requested more than once at the same time.</p>
</blockquote>
</aside>
<p>Yes, that’s consistent with the problems I found in the source.</p>
<aside class="quote no-group" data-username="leonardorame" data-post="16" data-topic="52602">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/l/f6c823/48.png" class="avatar"> leonardorame:</div>
<blockquote>
<p>In one of your answers you said <em>Try caching results instead of resources</em>, by this do you mean using <code>enif_make_binary</code> instead of <code>enif_make_resource</code>?</p>
</blockquote>
</aside>
<p>No, I meant putting <em>finished PNG images</em> into your <code>ImageCache</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272406" 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/long-running-c-nif-segfaults/52602/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-272406" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272406"
                     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="272412" data-post-id="272412">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="leonardorame" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  leonardorame
                    <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>Yes I thought about that. The problem is when I need to resize the original image or apply window/level (similar to brightness &amp; contrast) I will be forced to reload the image from disk, and that isn’t efficient.</p>
<p>I also was thinking about creating a binary of the image and pass back and forth between the NIF and the Elixir program, but that also adds overhead, because I must create in-memory copies of the whole image.</p>
<p>It must be a way to only share <strong>pointers</strong> to the data created by the NIF. The issue with that is that I must be very careful about memory leaks and race conditions.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272412" 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/long-running-c-nif-segfaults/52602/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-272412" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272412"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="272414" data-post-id="272414">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jhogberg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/120/24176_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jhogberg
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>To quote myself:</p>
<aside class="quote group-Erlang-Core-Team" data-username="jhogberg" data-post="13" data-topic="52602">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/48/24176_2.png" class="avatar"> jhogberg:</div>
<blockquote>
<p>or if they’re very dynamic, create a new process for each instance and store those in the cache. When a new request comes in, ask the cached process to scale the image.</p>
</blockquote>
</aside>
<p>To word it differently, spawn a process whose only job is to rescale a certain image, then put the pid of that process into <code>ImageCache</code>. The process would load the image from disk once, and use the same resource to rescale images when requested to do so.</p>
<p>If several requests for the same image come in concurrently, they would all ask the same process to do this, but you wouldn’t need to worry about races since the process only handles one request at a time, and would finish them in the order they came in.</p>
<p>Makes sense?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272414" 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/long-running-c-nif-segfaults/52602/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-272414" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272414"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="272415" data-post-id="272415">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="leonardorame" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  leonardorame
                    <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>Yes, of course that makes sense. Now I need to figure out how to do it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272415" 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/long-running-c-nif-segfaults/52602/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-272415" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272415"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="272597" data-post-id="272597">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="leonardorame" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  leonardorame
                    <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 again <a class="mention" href="/u/jhogberg" rel="nofollow">@jhogberg</a>, I’m implementing your suggested solution using an Agent, but the program still crashes when a call to createScaledImage, for the same SOPInstanceUID is made more than once at the same time.</p>
<p>Please take a look at the updated image function:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def image(conn, params) do                                                                                                                                                                                                                 
    sopinstanceuid = params["sopinstanceuid"]                                                                                                                                                                                                
    {width, ""} = Integer.parse(params["width"])                                                                                                                                                                                             
    {height, ""} = Integer.parse(params["height"])                                                                                                                                                                                           
    pid = ImageCache.get(sopinstanceuid)                                                                                                                                                                                                     
    if pid do                                                                                                                                                                                                                                
      IO.puts("====&gt; CACHED! &lt;====")                                                                                                                                                                                                         
      IO.puts "Pid: #{inspect pid}"                                                                                                                                                                                                          
      dcmff = Agent.get(pid, fn map -&gt; Map.get(map, :ff) end)                                                                                                                                                                                
      scaledImage = Dcmtknif.createScaledImage(dcmff, width, height, 0, 0);                                                                                                                                                                  
      IO.puts("====&gt; createScaledImage &lt;====")                                                                                                                                                                                               
      wlImage = Dcmtknif.setMinMaxWindow(scaledImage)                                                                                                                                                                                        
      IO.puts("====&gt; setMinMaxWindow &lt;====")                                                                                                                                                                                                 
      pngimage = Dcmtknif.getPNG(wlImage)                                                                                                                                                                                                    
      IO.puts("====&gt; getPNG &lt;====")                                                                                                                                                                                                          
      conn                                                                                                                                                                                                                                   
      |&gt; put_resp_content_type("image/png")                                                                                                                                                                                                  
      |&gt; send_resp(200, pngimage)                                                                                                                                                                                                            
    else                                                                                                                                                                                                                                     
      im = ApiController.image(sopinstanceuid)                                                                                                                                                                                               
      IO.puts(im.imagepath)                                                                                                                                                                                                                  
      if File.exists?(im.imagepath) do                                                                                                                                                                                                       
        IO.puts("====&gt; NOT CACHED, LOADING FROM FILE &lt;====")                                                                                                                                                                                 
        dicomfile = im.imagepath                                                                                                                                                                                                             
        IO.puts("Antes de load()")                                                                                                                                                                                                           
                                                                                                                                                                                                                                             
        {:ok, pid} = Agent.start_link(fn -&gt; %{} end)                                                                                                                                                                                         
        ImageCache.put(sopinstanceuid, pid)                                                                                                                                                                                                  
        Agent.update(pid, fn map -&gt; Map.put(map, :ff, Dcmtknif.load(dicomfile)) end)                                                                                                                                                         
        dcmff = Agent.get(pid, fn map -&gt; Map.get(map, :ff) end)                                                                                                                                                                              
        IO.puts("Antes de createScaledImage")                                                                                                                                                                                                
        scaledImage = Dcmtknif.createScaledImage(dcmff, width, height, 0, 0);                                                                                                                                                                
        IO.puts("Antes de setMinMaxWindow")                                                                                                                                                                                                  
        wlImage = Dcmtknif.setMinMaxWindow(scaledImage)                                                                                                                                                                                      
        pngimage = Dcmtknif.getPNG(wlImage)                                                                                                                                                                                                  
        conn                                                                                                                                                                                                                                 
        |&gt; put_resp_content_type("image/png")                                                                                                                                                                                                
        |&gt; send_resp(200, pngimage)                                                                                                                                                                                                          
      else                                                                                                                                                                                                                                   
        conn                                                                                                                                                                                                                                 
        |&gt; send_resp(404, "File not found")                                                                                                                                                                                                  
      end                                                                                                                                                                                                                                    
    end                                                                                                                                                                                                                                      
  end
</code></pre>
<p>When call createScaledImage once everything is ok, but if the same image is requested more than once, the Agent returns the correct PID, but apparently it is not isolated. Here’s the output just before the crash:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
====&gt; CACHED! &lt;====
[info] GET /image
====&gt; CACHED! &lt;====
[info] GET /image
[debug] Processing with Prueba3Web.PageController.image/2
  Parameters: %{"height" =&gt; "101", "sopinstanceuid" =&gt; "1.3.46.670589.11.76003.5.0.848.2022080115090557013", "width" =&gt; "101"}
  Pipelines: [:browser]
[debug] Processing with Prueba3Web.PageController.image/2
  Parameters: %{"height" =&gt; "101", "sopinstanceuid" =&gt; "1.3.46.670589.11.76003.5.0.848.2022080115090557013", "width" =&gt; "101"}
  Pipelines: [:browser]
Pid: #PID&lt;0.649.0&gt;
Pid: #PID&lt;0.649.0&gt;
====&gt; createScaledImage &lt;====
zsh: segmentation fault (core dumped)  mix phx.server
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="272597" 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/long-running-c-nif-segfaults/52602/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-272597" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="272597"
                     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 #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/52602/load_more?page=3">Load more posts (2 remaining)</a>
</div></template></turbo-stream>