efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Infinite loop in inconsistent dictionary at TryGetValue() while QueryCompiling

Open thumer opened this issue 6 years ago • 2 comments

We ran into the problem, that the query compling procedure of multiple queries at program start ran into a infinite loop, because a intern dictionary of the NodeTypeProvider seems to be corrupt. Here you can see, that i has the same value as entries[i].next - so this method will never finish: FindEntry_State NodeTypeProvider_Dictionary

I am sorry, that I cannot build an simple test project with a reproduction of the issue. So I try to give you some infos of our production code.

I analyzed our frozen application memory dump. I can see that we executing multiple queries at the same time in different threads. But they are using different DbContexts/Connections. The hanging queries are GetCurrentMitarbeiterAsync() and GetKontoBewegungenAsync().

Here are the Queries and CallStacks:

CallStack_GetCurrentMitarbeiter_Query
public async Task<MitarbeiterQueryEntity> GetCurrentMitarbeiterAsync()
{
    var mitarbeiterQuery = from m in DbContext.Mitarbeiter
                            join pg in DbContext.PersonGenerationen.OfType<NatuerlichePersonGeneration>() on m.PersonId equals pg.PersonId
                            join p in DbContext.Personen on m.PersonId equals p.Id
                            where m.Benutzername.Contains("\\") ? m.Benutzername.EndsWith("\\th") :
                                                            m.Benutzername.Contains("@") ? m.Benutzername.StartsWith("th@") :
                                                            m.Benutzername.Equals("th")
                            select new MitarbeiterQueryEntity
                            {
                                PersonGenerationId = pg.Id,
                                PersonId = pg.PersonId,
                                MitarbeiterId = m.Id,
                                Mitarbeiternummer = m.Mitarbeiternummer,
                                Kurzbezeichnung = m.Kurzbezeichnung,
                                Benutzername = m.Benutzername,
                                SecurityClearance = m.SecurityClearance,
                                Eintrittsdatum = m.Eintrittsdatum,
                                Austrittsdatum = m.Austrittsdatum,
                                MitarbeiterStammdatenSichtbar = m.MitarbeiterStammdatenSichtbar,
                                Vorname = pg.Vorname,
                                Nachname = pg.Nachname,
                                Geburtsdatum = pg.Geburtsdatum,
                                AkademGradVor = pg.AkademGradVor,
                                AkademGradNach = pg.AkademGradNach,
                                Titel = pg.Titel,
                                Beruf = pg.Beruf,
                                Svnr = pg.Svnr,
                                Familienstand = pg.Familienstand,
                                Geschlecht = pg.Geschlecht,
                                Qualifikation = m.Qualifikation,
                                Emails = pg.Emailadressen.Select(e => new MitarbeiterQueryEntity.Email() { Adresse = e.Emailadresse, Art = e.EmailArt, Anmerkung = e.Anmerkung }).ToList(),
                                Ort = pg.Anschriften.OrderBy(p => p.SortOrder).FirstOrDefault().Anschrift.Ort,
                                TelefonFaxNrs = pg.TelefonFaxNummern.Select(t => new MitarbeiterQueryEntity.TelefonFaxNr() { Nummer = t.TelefonFaxNr, Art = t.TelefonFaxNrArt, Anmerkung = t.Anmerkung }).ToList(),
                                RZLWinKlientenSichtbarkeit = m.RZLWinKlientenSichtbarkeit,
                                EinschraenkungAufVerwendung = p.EinschraenkungAufVerwendung,
                                BelegeAndererMitarbeiterLoeschen = m.BelegeAndererMitarbeiterLoeschen
                            };
    return await mitarbeiterQuery.FirstOrDefaultAsync().ConfigureAwait(false);
}
CallStack_GetKontoBewegungAsync_Query
public async Task<IList<JournalQueryEntity>> GetKontoBewegungenAsync(int kontoIdentifierId, long? buchungToExcludeId, IList<long> bewegungen = null)
{
    var queryBuchungen = from be in DbContext.Bewegungen
                            join ust in DbContext.UstLandProzentsaetze on be.SkontoPflichtkontoIdentifier.UstLandProzentsatzId equals ust.Id into us
                            from ust in us.DefaultIfEmpty() // left outer join für UstLandProzentsätze
                            where be.Sammelkonto.KontoIdentifierId == kontoIdentifierId &&
                                    be.BewegungsArt != BewegungsArt.Steuerbewegung && be.BewegungsArt != BewegungsArt.SteuerbewegungNichtAbzugsfaehig
                            orderby be.Buchung.Datum, be.Buchung.Id, be.SortOrder
                            select new JournalQueryEntity()
                            {
                                BuchungszeileId = be.Buchung.Id,
                                SplitbuchungId = be.Buchung is Splitbuchung ? be.BuchungId : ((Buchung)be.Buchung).SplitbuchungId,
                                BewegungId = be.Id,
                                BewegungPosition = be.SortOrder,
                                Datum = be.Buchung.Datum,
                                BelegNummer = be.Buchung.BelegNr,
                                Buchungstyp = be.Buchung.Buchungstyp,
                                BuchungHasBeenModified = be.Buchung.BuchungHasBeenModified,
                                Belegkreiskuerzel = be.Buchung.Belegkreiskuerzel,
                                Kontonummer = be.Sammelkonto.Kontonummer,
                                KontoId = be.KontoId,
                                KontoIdentifierId = be.Konto.KontoIdentifierId,
                                GegenKontonummer = be.Gegenkonto.Kontonummer,
                                OriginalBuchungtext = be.Buchung.Text,
                                BewegungsText = be.BewegungsText,
                                SkontoPflichtkontoIdentifierId = be.SkontoPflichtkontoIdentifierId,
                                SkontoPflichtkontoBezeichnung = be.SkontoPflichtkontoIdentifier != null ? be.SkontoPflichtkontoIdentifier.Bezeichnung : null,
                                UstLandProzentsatzBezeichnung = ust != null ? (string.IsNullOrEmpty(ust.AbweichendeAnzeige) ? ust.Wert.ToString() + "%" : ust.AbweichendeAnzeige) : null,
                                Steuercode = be.Steuercode,
                                UID = (be.Buchung is Buchung) ? ((Buchung)be.Buchung).UID : null,
                                Soll = be.SollBetrag,
                                Haben = be.HabenBetrag,
                                BewegungsArt = be.BewegungsArt,
                                JournalTyp = JournalTyp.VirtuelleBewegung,
                                Buchungsstatus = be.Buchung.Buchungsstatus,
                                InOffenerSplitbuchung = (be.Buchung is Splitbuchung ? !((Splitbuchung)be.Buchung).SplitbuchungVollstaendig : false)
                                                        || (be.Buchung is Buchung ? ((Buchung)be.Buchung).Splitbuchung != null && !((Buchung)be.Buchung).Splitbuchung.SplitbuchungVollstaendig : false),
                                PositionImStapel = be.Buchung.Position,
                                BuchungsstapelId = be.Buchung.BuchungsstapelId.Value, // Buchungen mit Bewegungen (also keine OP oder unvollständige Buchungen) haben immer einen Buchungsstapel
                                BuchungsstapelBezeichnung = be.Buchung.Buchungsstapel.Bezeichnung,
                                BuchungsstapelAnlagezeitpunkt = be.Buchung.Buchungsstapel.Erstellungszeitpunkt,
                                BuchungsstapelFinalisierungszeitpunkt = be.Buchung.Buchungsstapel.Finalisierungszeitpunkt,
                                BuchungsstapelArt = be.Buchung.Buchungsstapel.BuchungsstapelArt,
                                AnzahlBewegungen = 1,
                                UstJahrMonat = be.Buchung.UstJahrMonat
                            };

    return await queryBuchungen.ToListAsync().ConfigureAwait(false);
}

The dictionary-instance of both threads is the same. So i think, it was manipulated at the same time. The System.Collections.Generic.Dictionary is not threadsafe - so a ConcurrentDictionary would be better in this case. I know it's part of Remotion.Linq. Maybe an alternative would be locking some query compiling actions.

Is this an known issue? How can we avoid this?

Thanks in advance!

Further technical details

EF Core version: 2.2.6 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: NET Framework 4.7.2 Operating system: Windows 10 - 1909 IDE: Visual Studio 2019 16.4.2

thumer avatar Feb 06 '20 15:02 thumer

@thumer EF Core 2.2 is out-of-support. Please update to 3.1 and report back if you are still seeing this issue.

ajcvickers avatar Feb 08 '20 00:02 ajcvickers

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

ajcvickers avatar Feb 14 '20 17:02 ajcvickers