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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="alexandre" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/alexandre/120/678_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  alexandre
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If you find yourself in a similar situation, here is what worked so far:</p>
<ol>
<li>Run <code>mix test --trace</code> to see which test file has the test that triggers this error</li>
<li>Put a <code>setup</code> block on this test file with <code>on_exit(fn -&gt; :timer.sleep(1000) end)</code>. This will give sometime between the test runs. Since <code>on_exit</code> runs in another process, the test process will be able to finish normally.</li>
<li>Run <code>mix test test/app_name/problematic_test.exs --trace</code></li>
</ol>
<p>This will show the <code>DBConnection</code> error right below the problematic test.<br>
Also, since this is an intermittent error, it’s also useful to run the file multiple times with something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">$ for i in {1..10} do; mix test test/app_name/problematic_test.exs --trace done
</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="296639" data-batch-url="/posts/batch_likers">
                        2
                      </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/intermittent-dbconnection-connectionerror-in-tests/46037/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-296639" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="296639"
                     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="296640" data-post-id="296640">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dimitarvp" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/120/38664_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dimitarvp
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Did you manage to pinpoint the cause that way?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="296640" 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/intermittent-dbconnection-connectionerror-in-tests/46037/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-296640" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="296640"
                     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="296642" data-post-id="296642">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You can also use a fixed seed, so test always run in the same sequence.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="296642" 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/intermittent-dbconnection-connectionerror-in-tests/46037/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-296642" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="296642"
                     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="296696" data-post-id="296696">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="alexandre" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/alexandre/120/678_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  alexandre
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes! It was a <code>Phoenix.Channel</code> that received a message and ran a DB query after the test process finished.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="296696" data-batch-url="/posts/batch_likers">
                        2
                      </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/intermittent-dbconnection-connectionerror-in-tests/46037/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-296696" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="296696"
                     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="303634" data-post-id="303634">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="achedeuzot" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/achedeuzot/120/13972_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  achedeuzot
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>And sorry to ask but how did you fix it ?</p>
<p>Did you keep the waiting or did you find a better way (like listening for the DB query to finish or something ?).</p>
<p>Thanks for your insights <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="303634" 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/intermittent-dbconnection-connectionerror-in-tests/46037/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-303634" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="303634"
                     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="303667" data-post-id="303667">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mbklein" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mbklein/120/15951_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mbklein
                    <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">
								<aside class="quote no-group" data-username="achedeuzot" data-post="16" data-topic="46037">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/achedeuzot/48/13972_2.png" class="avatar"> achedeuzot:</div>
<blockquote>
<p>Did you keep the waiting or did you find a better way (like listening for the DB query to finish or something ?).</p>
</blockquote>
</aside>
<p>I found that some of my tests had GenServers running (started in the test setup using <code>start_supervised!/2</code>), and the GenServer was outliving the test’s sandbox repo connection. I had to make sure I had an <code>on_exit</code> handler that stopped the GenServer from continuing to try to use the database before the rest of the test teardown could happen.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="303667" data-batch-url="/posts/batch_likers">
                        4
                      </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/intermittent-dbconnection-connectionerror-in-tests/46037/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-303667" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="303667"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="303722" data-post-id="303722">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="achedeuzot" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/achedeuzot/120/13972_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  achedeuzot
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I finally found that my <code>DataCase</code> modules were still using a previous approach which used the following syntax:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">setup tags do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(MyModule.Repo)

    unless tags[:async] do
        Ecto.Adapters.SQL.Sandbox.mode(MyModule.Repo, {:shared, self()})
    end
end
</code></pre>
<p>and I replaced it with the new syntax:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">setup tags do
    MyModule.DataCase.setup_sandbox(tags)
    :ok
end

def setup_sandbox(tags) do
    pid = Ecto.Adapters.SQL.Sandbox.start_owner!(MyModule.Repo, shared: not tags[:async])
    on_exit(fn -&gt; Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end
</code></pre>
<p>and it seems to work a lot better <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="303722" data-batch-url="/posts/batch_likers">
                        2
                      </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/intermittent-dbconnection-connectionerror-in-tests/46037/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-303722" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="303722"
                     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="303723" data-post-id="303723">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="achedeuzot" data-post="18" data-topic="46037">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/achedeuzot/48/13972_2.png" class="avatar"> achedeuzot:</div>
<blockquote>
<p>and it seems to work a lot better <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>The new approach uses the <code>on_exit</code> functionality of ex_unit, which allows “nested” <code>on_exit</code> callbacks to be completed before stopping the sandbox for a test. The old approach would monitor the test process itself and stop the sandbox as soon as the test process stopped, which is before <code>on_exit</code> callbacks executed. That’s why the new api is to be prefered – even generally I’d say.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="303723" data-batch-url="/posts/batch_likers">
                        2
                      </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/intermittent-dbconnection-connectionerror-in-tests/46037/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-303723" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="303723"
                     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="304382" data-post-id="304382">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks for that.<br>
Using this approach I still having a lot of errors about disconnected.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Postgrex.Protocol (#PID&lt;0.610.0&gt;) disconnected: ** (DBConnection.ConnectionError) client #PID&lt;0.2513.0&gt; exited
</code></pre>
<p>Did you faced something similar?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="304382" 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/intermittent-dbconnection-connectionerror-in-tests/46037/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-304382" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="304382"
                     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="304433" data-post-id="304433">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="achedeuzot" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/achedeuzot/120/13972_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  achedeuzot
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I do have some of those still happening but the runs of tests on the CI vs local are quite different so it’s difficult to pinpoint the issue.</p>
<p>Before my change, none of the test runs would pass. Now they do pass but they randomly fail.</p>
<p>Locally I mostly have such errors:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[error] Postgrex.Protocol (#PID&lt;0.1316.0&gt;) disconnected: ** (DBConnection.ConnectionError) client #PID&lt;0.19767.0&gt; exited
</code></pre>
<p>or</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[error] Child #Reference&lt;0.3366742383.3747086337.132479&gt; of Supervisor #PID&lt;0.14032.0&gt; (Supervisor.Default) shut down abnormally
** (exit) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
Pid: #PID&lt;0.14034.0&gt;
Start Call: Phoenix.LiveView.Channel.start_link/?
</code></pre>
<p>Whereas on the CI, I have errors related to the <code>assert_value</code> package:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[error] GenServer AssertValue.Server terminating
** (stop) exited in: GenServer.call(#PID&lt;0.1074.0&gt;, :flush, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir 1.15.6) lib/gen_server.ex:1074: GenServer.call/3
    (assert_value 0.10.1) lib/assert_value/server.ex:68: AssertValue.Server.handle_cast/2
    (stdlib 5.1) gen_server.erl:1103: :gen_server.try_handle_cast/3
    (stdlib 5.1) gen_server.erl:1165: :gen_server.handle_msg/6
    (stdlib 5.1) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_cast", {:flush_ex_unit_io}}

[error] Process AssertValue.Server (#PID&lt;0.903.0&gt;) terminating
** (exit) exited in: GenServer.call(#PID&lt;0.1074.0&gt;, :flush, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir 1.15.6) lib/gen_server.ex:1074: GenServer.call/3
    (assert_value 0.10.1) lib/assert_value/server.ex:68: AssertValue.Server.handle_cast/2
    (stdlib 5.1) gen_server.erl:1103: :gen_server.try_handle_cast/3
    (stdlib 5.1) gen_server.erl:1165: :gen_server.handle_msg/6
    (stdlib 5.1) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
Initial Call: AssertValue.Server.init/1
Ancestors: [#PID&lt;0.902.0&gt;, #PID&lt;0.901.0&gt;]
</code></pre>
<p>I’m still trying to find why I get some of these random errors.</p>
<p>They mostly appeared when switching to Elixir 1.15.6 (+ erlang 26.1) and upgrading some packages like <code>phoenix_live_view</code> to 0.18.18.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="304433" 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/intermittent-dbconnection-connectionerror-in-tests/46037/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-304433" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="304433"
                     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/46037/load_more?page=3">Load more posts (9 remaining)</a>
</div></template></turbo-stream>