Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataConnection fails to dispose in async scenarious #711

Open
burningice2866 opened this issue Nov 28, 2019 · 3 comments
Open

DataConnection fails to dispose in async scenarious #711

burningice2866 opened this issue Nov 28, 2019 · 3 comments
Labels

Comments

@burningice2866
Copy link
Contributor

Consider the following code block

using (var data = new DataConnection()) 
{
    var client = new HttpClient();
    var response = await client.GetStringAsync(url).ConfigureAwait(false);

    data.Add(something);
}

There is a risk that C1 will fail when disposing data since we can end up on a different threadpool-thread after out await.

When this happens the stacktrace looks like this

ThreadDataManager.Current points to a different thread data object!!!
  at Composite.Core.Threading.ThreadDataManager.ThreadDataManagerScope.Dispose(Boolean disposing)    
  at Composite.Core.Threading.ThreadDataManager.ThreadDataManagerScope.Dispose()    
  at Composite.Core.Implementation.DataConnectionImplementation.Dispose(Boolean disposing)    
  at Composite.Core.Implementation.DataConnectionImplementation.Dispose()    
  at Composite.Core.Implementation.ImplementationContainer`1.DisposeImplementation()    
  at Composite.Data.DataConnection.Dispose(Boolean disposing)    
  at Composite.Data.DataConnection.Dispose()    
  at Sermersooq.Data.CommitteeMeetingsService.<GetFutureMeetingsAsync>d__2.MoveNext() in E:\Work\Dot GL\Sermersooq\Website\code\Sermersooq\Data\CommitteeMeetingsService.cs:line 65 

--- End of stack trace from previous location where exception was thrown ---    
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()    
  at Sermersooq.ActivityCalendar.CommitteeMeetingsActivityCalendarProvider.<GetActivitiesAsync>d__3.MoveNext() in E:\Work\Dot GL\Sermersooq\Website\code\Sermersooq\ActivityCalendar\CommitteeMeetingsActivityCalendarProvider.cs:line 35 

--- End of stack trace from previous location where exception was thrown ---    
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()    
  at Sermersooq.ActivityCalendar.ActivityCalendarService.<GetActivitiesAsync>d__2.MoveNext() in E:\Work\Dot GL\Sermersooq\Website\code\Sermersooq\ActivityCalendar\ActivityCalendarService.cs:line 22
@burningice2866
Copy link
Contributor Author

A workaround is to not use await within a DataConnection scope, either by limiting scope size or reverting to use DataFacade for each data operation

@napernik
Copy link
Contributor

napernik commented Oct 2, 2020

One of the ways would be for C1 to start keeping the context in AsyncLocal, there're some issues/breaking changes with inheriting context where it should be inherited. That is something we will look into at some point

@burningice2866
Copy link
Contributor Author

burningice2866 commented Jan 7, 2022

A more high-level question while we are on this subject... what are the advantages of using DataConnection over DataFacade, if any? Is there any other reason than to wrap PublicationScope and CultureInfo rather than setting DataScope manually?

Is any there difference to doing this

using (new DataScope(PublicationScope.Published))
{
   DataFacade.GetData<IPage>();
}

rather than this

using (var data = new DataConnection(PublicationScope.Published))
{
   data.Get<IPage>()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants