danieleteti / delphimvcframework

DMVCFramework (for short) is a popular and powerful framework for WEB API in Delphi. Supports RESTful and JSON-RPC WEB APIs development.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TMVCConnectionsRepository - fCurrentConnectionsByThread: TDictionary<TThreadID, string>;

biware-repo opened this issue · comments

TMVCConnectionsRepository.RemoveConnection does not remove connection from fCurrentConnectionsByThread.

The new default connection is added by every operation within ActiveRecord class, but it is not removed from dictionary after finished operation in TMVCConnectionsRepository.RemoveConnection procedure.

The list in fCurrentConnectionsByThread stays unchanged. Moreover by every request there is still new ThreadID wich pointing to actual thread but the list is keeping all old thread IDs.

According to this #563 (comment) I have found possible reason of AV.

So I had to edit MVCFramework.ActiveRecord.pas to temporary solve this issue.

procedure TMVCConnectionsRepository.RemoveConnection(const aName: string;
const RaiseExceptionIfNotAvailable: Boolean = True);
var
  lName: string;
  lKeyName: string;
  lConnHolder: TConnHolder;
begin
  lName := aName.ToLower;
  lKeyName := GetKeyName(lName);

  fMREW.BeginWrite;
  try
    if not fConnectionsDict.TryGetValue(lKeyName, lConnHolder) then
    begin
      if RaiseExceptionIfNotAvailable then
      begin
        raise Exception.CreateFmt('Unknown connection %s', [aName])
      end
      else
      begin
        Exit;
      end;
    end; 

    fConnectionsDict.Remove(lKeyName);

   //ADDED to remove actual connection from thread dictionary - issue 563
   fCurrentConnectionsByThread.Remove(TThread.CurrentThread.ThreadID);


    try
      FreeAndNil(lConnHolder);
    except
      on E: Exception do
      begin
        LogE('ActiveRecord: ' + E.ClassName + ' > ' + E.Message);
        raise;
      end;
    end;
  finally
    fMREW.EndWrite;
  end;
end;

Thanks! Fixed.