landgraf-dev / aws-sdk-delphi

AWS (Amazon Web Services) SDK for Delphi.
Other
125 stars 31 forks source link

AV using SNS with xData (w/Aurelius) server #6

Closed LeifEirik closed 7 months ago

LeifEirik commented 2 years ago

Hi Wagner,

Using the SNS with Delphi 10.4.2, TMS xData 5.5.0.0, Aurelius 5.8.0.0 I have come across an AV that I'm unable to work around. I might be wrong, but to me it seems to only happen when used on xData Rest server for some strange reason.

If I run this code on new Delphi vcl project, all works as expected:

AWSClient: IAmazonSimpleNotificationService; lAWSTopicARN: string; begin { CREATE AWS SNS Topic } AWSClient := TAmazonSimpleNotificationServiceClient.Create(AWS_ACCESS_KEY, AWS_SECRET_KEY); lAWSTopic.Arn := AWSClient.CreateTopic('NEW_TOPIC_FOR_TEST').TopicArn; end;

...but if done on my rest server if gives me an AV. On my rest server I have a model where one entity is TAWSTopic, and I use the TAureliusModelEvents component and have implemented the OnInserting event as below:

{ Inserting } procedure TAppModelEvents.ameMainInserting(Sender: TObject; Args: TInsertingArgs); var lAWSTopic: TAWSTopic; AWSClient: IAmazonSimpleNotificationService; lAWSTopicARN: string;

begin if Args.Entity is TAWSTopic then begin lAWSTopic := TAWSTopic(Args.Entity);

{ CREATE AWS SNS Topic }
AWSClient := TAmazonSimpleNotificationServiceClient.Create(AWS_ACCESS_KEY, AWS_SECRET_KEY);
lAWSTopic.Arn := AWSClient.CreateTopic('NEW_TOPIC_FOR_TEST').TopicArn;

end; end;

Now if I debug from the very last "end;" for the event implementation, and just do F7 until AV, I get to the file AWS.RunTime.Credentials, and at the exit of destructor TBasicAWSCredentials.Destroy I get an AV.

destructor TBasicAWSCredentials.Destroy; begin inherited; end; <---- At the exit of this is where I get the AV

wlandgraf commented 2 years ago

Hi @LeifEirik. An AV at the end block hints something related to interface reference counting, i.e., an object implementing an interface is being destroyed. That's my guess but it's hard to know for now.

  1. Is it easily reproducible, does it happen all the time?
  2. Do you have the call stack at the moment of the error?
  3. Is the source code you provided the full source code of the event handler method?

Can you please put a breakpoint in the TBasicAWSCredentials.Destroy and check if it executes twice and, if yes, the call stack you get at both moments?

LeifEirik commented 2 years ago

Hi @wlandgraf ,

  1. Yes I think it is easy to reproduce, and it is happening every time. So it seem to be consistent.
  2. I can provide the call stack, but maybe it is easier to see my "How to reproduce AV" below?
  3. Yes, that was the full source code, well except that my AWS key where not shown :)

Unsure if the TBasicAWSCredentials.Destroy executes twice, the AV occurs the first time it is executed.

How to reproduce: An easy way to see the error is to use a TMS xData demo project that comes with xData/Aurelius:

  1. Load the ...\TMS XData\Demos\web\music\XDataMusicServer.dproj
  2. Add the correct environment variable to Delphi (Tool-Options-IDE-Environment Variables). In my case it is set like this: AWS_REGION = us-east-1
  3. Open the Api.Server.Module and drop a TAureliusModelEvents (I named mine: ameMain) component on this datamodule.
  4. Implement the ameMain.OnInserting like this:

uses ...., AWS.SNS, AWS.SNS.Client;

procedure TApiServerModule.ameMainInserting(Sender: TObject; Args: TInsertingArgs); const AWS_ACCESS_KEY = 'Your_Access_Key_Here'; AWS_SECRET_KEY = 'Your_Secrey_Key_Here'; var AWSClient: IAmazonSimpleNotificationService; begin { CREATE AWS SNS Topic } AWSClient := TAmazonSimpleNotificationServiceClient.Create(AWS_ACCESS_KEY, AWS_SECRET_KEY); AWSClient.CreateTopic('NEW_TOPIC_FOR_TEST').TopicArn; end;

  1. Put a breakpoint inside the OnInserting event, and run the application. At startup the OnInserting event will be triggere and the AV occurs as you step (F7/F8) out of the event implementation.
wlandgraf commented 2 years ago

Thanks for the detailed response, @LeifEirik. But, unfortunately, I cannot reproduce it. Everything works just fine here. I have tested it in both Delphi 11 and Delphi 10.4.2. Have you applied all updates to your Delphi? The 10.4.2 version I'm testing is 27.0.40680.4203.