Coder Social home page Coder Social logo

Comments (6)

Tratcher avatar Tratcher commented on June 19, 2024

There shouldn't be any relation between session and keep-alives. Can you share the application logs for before and after?

from session.

tcuk avatar tcuk commented on June 19, 2024

To produce these logs I launched ASP.NET core behind my nginx reverse proxy, opened a new incognito browser window and requested the same URL three times via a URL pointing to nginx. I used a single session variable which was a counter being incremented. In the failure case the counter does not persist and remains at 1.

Working case (Connection = keep-alive):

Running aspnet
[?1h=Hosting environment: Production
Content root path: /dotnetapp
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://remove/  
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method Woo.Web.Controllers.HomepageController.Index (Woo.Web) with arguments ((n
ull)) - ModelState is Valid
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ContentResultExecutor[1]
      Executing ContentResult with HTTP Response ContentType of text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Woo.Web.Controllers.HomepageController.Index (Woo.Web) in 840.0717ms
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 1111.6613ms 200 text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://remove/  
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method Woo.Web.Controllers.HomepageController.Index (Woo.Web) with arguments ((n
ull)) - ModelState is Valid
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ContentResultExecutor[1]
      Executing ContentResult with HTTP Response ContentType of text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Woo.Web.Controllers.HomepageController.Index (Woo.Web) in 4.0174ms
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 7.6ms 200 text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://remove/  
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method Woo.Web.Controllers.HomepageController.Index (Woo.Web) with arguments ((n
ull)) - ModelState is Valid
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ContentResultExecutor[1]
      Executing ContentResult with HTTP Response ContentType of text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Woo.Web.Controllers.HomepageController.Index (Woo.Web) in 2.7991ms
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 5.4859ms 200 text/plain; charset=utf-8

Failure case (Connection = close):

Running aspnet
[?1h=Hosting environment: Production
Content root path: /dotnetapp
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://remove/  
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method Woo.Web.Controllers.HomepageController.Index (Woo.Web) with arguments ((n
ull)) - ModelState is Valid
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ContentResultExecutor[1]
      Executing ContentResult with HTTP Response ContentType of text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Woo.Web.Controllers.HomepageController.Index (Woo.Web) in 881.3667ms
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Session.SessionMiddleware[10]
      Committing the session was canceled.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 1162.6012ms 200 text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://remove/  
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method Woo.Web.Controllers.HomepageController.Index (Woo.Web) with arguments ((n
ull)) - ModelState is Valid
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ContentResultExecutor[1]
      Executing ContentResult with HTTP Response ContentType of text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Woo.Web.Controllers.HomepageController.Index (Woo.Web) in 3.7355ms
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Session.SessionMiddleware[10]
      Committing the session was canceled.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 6.014ms 200 text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://remove/  
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method Woo.Web.Controllers.HomepageController.Index (Woo.Web) with arguments ((n
ull)) - ModelState is Valid
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ContentResultExecutor[1]
      Executing ContentResult with HTTP Response ContentType of text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Woo.Web.Controllers.HomepageController.Index (Woo.Web) in 11.195ms
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Session.SessionMiddleware[10]
      Committing the session was canceled.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 12.8115ms 200 text/plain; charset=utf-8
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]

My test controller:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Text;

namespace Woo.Web.Controllers
{
	[Route("")]
	public class HomepageController : Controller
	{
		private const string CountKey = "Count";

		[Route("")]
		public IActionResult Index()
		{
			var counter = Counter++;
			
			return Content($"I have counted to {counter}. " + DateTime.Now + " / " + HttpContext.Session.Id + " / " + Request.Headers["Connection"]);
		}

		private int Counter
		{
			get
			{
				return HttpContext.Session.GetInt32(CountKey) ?? 1;
			}
			set
			{
				HttpContext.Session.SetInt32(CountKey, value);
			}
		}
	}
}

from session.

Tratcher avatar Tratcher commented on June 19, 2024

Ah. The response has a content-length so the client can disconnect as soon as it gets the last byte, which is before the session gets committed. Kestrel's client disconnect notice triggers, which triggers RequestAborted, which prevents the session from being committed.

This appears to be an order of operations bug in Kestrel. The complete request has been received and the complete response has been sent so if the client disconnects then it's out of scope of the request and it should't trigger RequestAborted. I'll move this issue to the Kestrel repo.

Workaround: call session.CommitAsync() when you're done modifying the session.

from session.

Tratcher avatar Tratcher commented on June 19, 2024

This issue was moved to aspnet/KestrelHttpServer#2195

from session.

Tratcher avatar Tratcher commented on June 19, 2024

After some discussion we've decided to change both Session and Kestrel. Session will no longer depend on RequestAborted for commits, only it's normal timeout.

await feature.Session.CommitAsync(context.RequestAborted);

from session.

aspnet-hello avatar aspnet-hello commented on June 19, 2024

This issue was moved to dotnet/aspnetcore#2395

from session.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.