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

Add Async method to SchemaBuilder to prevent possible thread starvation #508

Merged
merged 28 commits into from
Nov 15, 2023

Conversation

MikeAlhayek
Copy link
Collaborator

@MikeAlhayek MikeAlhayek commented Nov 13, 2023

Fix #509

Note, there was a bug in the dispose logic of the Session class. The method CommitOrRollbackTransactionAsync has slightly different logic than CommitOrRollbackTransaction. I believe the logic in CommitOrRollbackTransaction is better and used.

@MikeAlhayek MikeAlhayek changed the title Add Async methods on the ISchemaBuilder Add Async method to SchemaBuilder to prevent possible thread starvation Nov 13, 2023
samples/YesSql.Samples.Web/Startup.cs Outdated Show resolved Hide resolved
src/YesSql.Core/Services/DbBlockIdGenerator.cs Outdated Show resolved Hide resolved
src/YesSql.Core/Services/DbBlockIdGenerator.cs Outdated Show resolved Hide resolved
src/YesSql.Core/Sql/SchemaBuilder.cs Show resolved Hide resolved
src/YesSql.Core/Store.cs Show resolved Hide resolved
@sebastienros
Copy link
Owner

Unit tests are failing since adding Rollabck in the DbGenerator, only on Sql, maybe the connection is already disposing the transaction. We need to understand what is happening. I am not sure we should just remove it since it's working for other databases. But that's a safety net.

@sebastienros
Copy link
Owner

Are you getting lazy, or just bored?

@MikeAlhayek
Copy link
Collaborator Author

Are you getting lazy, or just bored?

I wish I was board and have nothing else better to do.

@MikeAlhayek
Copy link
Collaborator Author

@sebastienros I think all the issues are now fixed.

  • After modifying the CommitOrRollbackTransactionAsync logic, await using var session = ... works as expected and all tests now await and pass.
  • In the DbBlockIdGenerator, I added the RollbackAsync() call you requested and it now worked. To get that to work, for some reason I had to set throwOnError to true when initializing SchemaBuilder instance.
  • I also changed all the BeginTransaction calls to BegingTransactonAsync

Now, everything support async which is great! Let me know if you see anything else or if you good with merging this one.

@MikeAlhayek MikeAlhayek changed the base branch from main to release/4.0 November 15, 2023 00:44
@MikeAlhayek MikeAlhayek merged commit 5dfa15e into release/4.0 Nov 15, 2023
1 check passed
@MikeAlhayek MikeAlhayek deleted the ma/add-async-methods branch November 15, 2023 00:47
@@ -630,7 +630,7 @@ public void Dispose(bool disposing)
{
try
{
CommitOrRollbackTransaction();
CommitOrRollbackTransactionAsync().ConfigureAwait(false).GetAwaiter().GetResult();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebastienros

Don't remember the details around this but are you sure about using ConfigureAwait(false) before GetAwaiter()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it just handles the error differently.

Do you see an issue?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would need more time to remember the details so not sure but reading again https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

For example this and that

Using ConfigureAwait(false) to avoid deadlocks is a dangerous practice.

The top-level methods, meanwhile, do require the context, so they cannot use ConfigureAwait(false)

I would not have used .ConfigureAwait(false) here but inside CommitOrRollbackTransactionAsync(), applied on the async call that it does.

Which may be useless if it is called asynchronously but maybe better for this synchronous usage.

But not sure ;)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the doubt I would not have used it at all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I'll remove it tomorrow before releasing the new version

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtkech what about in the other places like Session.Save and the SchemeBuilder?

Copy link
Collaborator

@jtkech jtkech Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem for me.

Otherwise we may need to use it on each async call everywhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, you suggest removing .ConfigureAwait(false) everywhere it's used? So .GetAwaiter().GetResult() only?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for now

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtkech done via #511

Let me know if you need anything else modified

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

Successfully merging this pull request may close these issues.

Add Async methods to the SchemaBuilder
3 participants