Code Monkey home page Code Monkey logo

Comments (3)

luigiberrettini avatar luigiberrettini commented on June 28, 2024

The issue reported by @sanderweltje highlights how easy it is to write bug-free concurrent code.

The continuation used in the ProcessQueueAsync(MessageBuilder messageBuilder) method might seem the proper solution to take care of any exception raised in the ProcessQueueAsync(MessageBuilder messageBuilder, TaskCompletionSource<object> tcs) method:

private void ProcessQueueAsync(MessageBuilder messageBuilder)
{
    ProcessQueueAsync(messageBuilder, new TaskCompletionSource<object>())
        .ContinueWith(t =>
        {
            InternalLogger.Warn(t.Exception?.GetBaseException(), "[Syslog] ProcessQueueAsync faulted within try");
            ProcessQueueAsync(messageBuilder);
        }, token, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Current);
}

Unfortunately due to the fact that the same CancellationToken is used everywhere, the ContinueWith does not execute the continuation function if the token has been cancelled.

from nlog.targets.syslog.

sanderweltje avatar sanderweltje commented on June 28, 2024

If the CancellationToken is used to cancel all processing, the reuse of the token is correct. If the CancellationToken is also used for other purposes, a second CancellationToken might be a better solution. In case you want to cancel all processing and an exception is thrown, you probably don't want to restart ProcessQueueAsync(messageBuilder)

from nlog.targets.syslog.

luigiberrettini avatar luigiberrettini commented on June 28, 2024

I already use multiple tokens for different purposes, but the issue was caused by the one used to stop processing, because it was passed to all ContinueWith, preventing exceptions from being observed if thrown after a cancellation.

Considering the logical workflow it does not make sense to continue processing when a cancellation is triggered, but, due to the way ContinueWith interacts with CancellationToken (here some tests), relying on the first successful detection of the cancellation i.e. if (token.IsCancellationRequested), allows to achieve the expected graceful termination.

from nlog.targets.syslog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.