title: SignalR connection causes gateway timeout
body: <p>I'm at a bit of a loss on this one, been debugging it for days and I'm baffled.</p>
  <p>I have an ASP.NET 6 API with SignalR.</p>
  <pre class="lang-cs prettyprint-override"><code>builder.Services.AddSignalR()
         .AddHubOptions&lt;TestHub&gt;(x =&gt;
         {
             x.EnableDetailedErrors = true;
         })
         .AddStackExchangeRedis(dbCon.Redis, options =&gt;
         {
             ...
         })
         .AddJsonProtocol(options =&gt;
         {
             ...
         });
  ...
  app.MapStatusEndpoints();
  app.MapControllers();
  app.MapHub&lt;TestHub&gt;(&quot;/api/h&quot;);
  </code></pre>
  <p>Locally this all works perfectly without issue.</p>
  <p>When I throw it up on the server with nginx (AWS with t4g.small, so arm64), for some reason after a web socket connection is established, any api call from the same page causes nginx to return gateway timeouts, and causes SignalR to disconnect for roughly 1 minutes, the connection reconnects, api call fails, gateway timeouts again. Even if the api call is</p>
  <p>I've looked through the logs on nginx and all I see is:</p>
  <blockquote>
  <p>2022/09/24 16:18:30 [error] 41453#41453: *17989 connect() failed (111: Connection refused) while connecting to upstream, client: 10.30.4.89, server: test.<em>SNIP</em>.com, request: &quot;GET /api/h?access_token=<em>SNIP</em> HTTP/1.1&quot;, upstream: &quot;http:// 127.0.0.1:5010/api/h?access_token=<em>SNIP</em>&quot;, host: &quot;test.<em>SNIP</em>.com&quot;</p>
  </blockquote>
  <blockquote>
  <p>2022/09/24 16:25:31 [error] 41453#41453: *18087 connect() failed (111: Connection refused) while connecting to upstream, client: 10.30.4.89, server: test.<em>SNIP</em>.com, request: &quot;POST /api/start HTTP/1.1&quot;, upstream: &quot;http:// 127.0.0.1:5010/api/start&quot;, host: &quot;test.<em>SNIP</em>.com&quot;, referrer: &quot;https: //test.<em>SNIP</em>.com/arena&quot;</p>
  </blockquote>
  <p>I think* it's Kestrel/SignalR rejecting the request, but I don't understand why, I can't find any logs to indicate why...</p>
  <p>I more or less trimmed my nginx site back to almost bare minimum...</p>
  <pre><code>server {
      listen        80;
      server_name   test.*snip*.com;
      server_tokens off;
      
      location / {
          root /var/www/test.*snip*.com/deployments/0.0.0/vue;
          index index.html;
          try_files $uri /index.html;
      }
  
      location /api/ {
          proxy_pass         http://127.0.0.1:5010;
          proxy_http_version 1.1;
          proxy_set_header   Upgrade $http_upgrade;
          proxy_set_header   Connection 'keep-alive, upgrade';
          proxy_set_header   Host $host;
          proxy_cache_bypass $http_upgrade;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Proto $scheme;
          proxy_set_header   Host $host;
          proxy_cache off;
          proxy_buffering off;
      }
  }
  </code></pre>
  <p>A solution would be nice, but looking for idea's on how to figure out what the problem is. No exceptions are raised from the app.</p>
  <p>Update: I found the <code>journalctl</code> logs and it appears that System.Text.Json causes a StackOverflow when on a t4g.small instance.</p>
  <p><a href="https://i.stack.imgur.com/v8TX3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v8TX3.png" alt="enter image description here" /></a></p>
  <p>If I keep STJ and use an amd instance (t3a.small) it works.</p>
  <p>If I change to newtonsoft.json and use a t4g.small it works.</p>
  <p>But STJ + t4g.small it fails when using <code>Dictionary&lt;string, object&gt;</code></p>
  <p>The JSON looks like:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;action&quot;: &quot;Battle&quot;,
      &quot;data&quot;: {
          &quot;Level&quot;: 10
      }
  }
  
  </code></pre>
  <p>And the class</p>
  <pre><code>public sealed class Action
  {
      public int Id { get; set; }
      public ActionTypes ActionType { get; set; } // enum
      public int UserId { get; set; }
      public Dictionary&lt;string, string&gt; Data { get; set; } = new();
  }
  </code></pre>
  <p>So it's nothing out of the ordinary... Unsure why STJ throws up on this.</p>