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

Make concurrency check handle re-entrance #7375

Closed
ghost opened this issue Jan 7, 2017 · 14 comments
Closed

Make concurrency check handle re-entrance #7375

ghost opened this issue Jan 7, 2017 · 14 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. punted-for-2.0 punted-for-2.1 type-enhancement
Milestone

Comments

@ghost
Copy link

ghost commented Jan 7, 2017

Getting exception below when making Async calls.

Exception message:
A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.

Stack trace:
   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.<EnterCriticalSectionAsync>d__6.MoveNext()
--- 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.TaskAwaiter`1.GetResult()
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext()
--- 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.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.Identity.RoleManager`1.<RoleExistsAsync>d__34.MoveNext()
                    roleManager.CreateAsync(new ApplicationRole() { Name = role.Key, NormalizedName = role.Key.ToUpper(), System = true, Description = role.Value });
    }
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore.Design": {
  "type": "build",
  "version": "1.1.0"
},
@jnm2
Copy link

jnm2 commented Jan 7, 2017

This is a common user error. No two sync or async operations can be in flight at the same time.

@ghost ghost closed this as completed Jan 8, 2017
@wgutierrezr
Copy link

wgutierrezr commented Jun 15, 2017

I have the same issue.

            var ret = _context.AppProgramMenus.Where(p =>
                    p.Active == true &&
                    (p.Program.Title.Contains(searchString) || p.Menu.Title.Contains(searchString)))
                    .Select(p =>
                        new RolMenuProgram
                        {
                            Id = p.ProgramMenuId,
                            ProgramTitle = p.Program.Title,
                            MenuTitle = p.Menu.Title,
                            canCreate = GetCRUD(p.ProgramMenuId, roleid, "C"),
                            canRead = GetCRUD(p.ProgramMenuId, roleid, "R"),
                            canUpdate = GetCRUD(p.ProgramMenuId, roleid, "U"),
                            canDelete = GetCRUD(p.ProgramMenuId, roleid, "D")
                        });
        private bool GetCRUD(int programMenuId, int? roleId, string crud)
        {
            var ret = false;
            var rmp = _context.AppRoleMenuPrograms.Where(r => r.ProgramMenuId == programMenuId && r.RoleId == roleId).SingleOrDefault(); **** ERROR SHOWS HERE ****
            switch (crud)
            {
                case "C":
                    ret = rmp != null ? rmp.AllowCreate : false;
                    break;
                case "R":
                    ret = rmp != null ? rmp.AllowRead : false;
                    break;
                case "U":
                    ret = rmp != null ? rmp.AllowUpdate : false;
                    break;
                case "D":
                    ret = rmp != null ? rmp.AllowDelete : false;
                    break;
            }

            return ret;
        }

@jnm2
Copy link

jnm2 commented Jun 15, 2017

@wgutierrezr It means you're trying to do two database operations at the same time. It's your job to make sure that doesn't happen. If you need to overlap database calls, you need two DbContexts.

@wgutierrezr
Copy link

Hi, Using two context I had the same issue.

@jnm2
Copy link

jnm2 commented Jun 15, 2017

@wgutierrezr Oh, I see your problem. While you're trying to load data from _context via _context.AppProgramMenus.Where(...).Select(...), at the same time you're trying to load data from _context by calling the GetCRUD method during the .Select(...) for the first data load.

You need to change your first query from
_context.AppProgramMenus.Where(...).Select(...) to _context.AppProgramMenus.Where(...).ToList().Select(...).

The .ToList() makes sure the first query is finished and all the results are in a list before it goes on to run the .Select(...) which calls GetCRUD and runs more queries.

@ajcvickers
Copy link
Member

@divega @anpete Should we try to make this work? It's not multiple threads, but rather one query that has calls that are (I assume) funcletized and themselves execute queries.

@ajcvickers ajcvickers reopened this Jun 15, 2017
@ajcvickers ajcvickers changed the title A second operation started on this context before a previous operation completed Make concurrency check handle re-entrance Jun 15, 2017
@ajcvickers ajcvickers added this to the 2.0.0 milestone Jun 15, 2017
AndriySvyryd added a commit that referenced this issue Jun 15, 2017
AndriySvyryd added a commit that referenced this issue Jun 15, 2017
AndriySvyryd added a commit that referenced this issue Jun 15, 2017
AndriySvyryd added a commit that referenced this issue Jun 15, 2017
AndriySvyryd added a commit that referenced this issue Jun 16, 2017
AndriySvyryd added a commit that referenced this issue Jun 16, 2017
AndriySvyryd added a commit that referenced this issue Jun 17, 2017
@AndriySvyryd
Copy link
Member

We need to investigate the consequences of doing this further to avoid issues like #8864

@ajcvickers
Copy link
Member

Note from triage: Since using the context inside client eval of the function is blocked by this issue, and this is in turn blocked by #8864, @anpete is going to investigate #8864 again to see whether we can do something for 2.1. However, #8864 looks quite hard, so we will cycle back again after the investigation and decide how to proceed.

@ajcvickers
Copy link
Member

Fixed in #17089

@wgutierrezr
Copy link

Thank you to every body, you help me alot.

@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview9 Aug 21, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview9, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. punted-for-2.0 punted-for-2.1 type-enhancement
Projects
None yet
Development

No branches or pull requests

7 participants