Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

MagickImage.ReadAsync swallows non cancellation related exceptions from the stream it reads from

Aperta Adatta ai principianti
#2,097 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
backend

Direzione di ricerca

Inizia individuando AsyncStreamWrapper.ReadAsync e il percorso di MagickImage.ReadAsync che lo attende. Riproduci il problema con il test ThrowingStream fornito, quindi verifica che la relativa IOException raggiunga il chiamante mentre la cancellazione continui a comportarsi come previsto. Il lavoro è completato quando il test passa senza restituire un’immagine vuota.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Magick.NET version

14.17.2

Environment (Operating system, version and so on)

Mac / Linix

Description

AsyncStreamWrapper has this code:

    private async Task ReadAsync(CancellationToken cancellationToken)
    {
        while (true)
        {
            try
            {
                await _performRead.WaitAsync(cancellationToken).ConfigureAwait(false);
            }
            catch
            {
                _exceptionThrown = true;
                if (_performRead.Wait(0))
                    _readDone.Release();
                return;
            }

            if (_readCount == -1)
                return;

            try
            {
                _readCount = await _stream.ReadAsync(Data, 0, _readCount, cancellationToken).ConfigureAwait(false);
            }
            catch
            {
                _exceptionThrown = true;
            }
            finally
            {
                _readDone.Release();
            }
        }
    }

if _stream.ReadAsync throws the fact is logged so that subsequently in the caller the cancellation token has an opportunity to throw.

        var readTask = ReadAsync(cancellationToken);

        await Task.WhenAll(actionTask, readTask).ConfigureAwait(false);

        if (_exceptionThrown)
            cancellationToken.ThrowIfCancellationRequested();
    }

However this has an unfortunate side effect, and that is that non-cancellation related exceptions are swallowed. I discovered this because I use a wrapper around my network streams that throw under certain conditions. When this wrapper throws MagickImage.ReadAsync returns an RGB image of 0x0 dimensions rather than allowing the exception to percolate up.

Steps to Reproduce
    class ThrowingStream : MemoryStream
    {
        public ThrowingStream(byte[] buffer, bool writable) : base(buffer, writable)
        {
        }

        public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            throw new IOException("Space Goat Failure");
        }
    }
    
    [TestMethod]
    public async Task SafeReadAsync_HealthyStream_ReadsTheImageBad()
    {
        using var image = new MagickImage();
        byte[] input = [1, 2, 3, 4, 5];

        await Assert.ThrowsAsync<Exception>(() => image.ReadAsync(new ThrowingStream(input, false)));
    }

The test fails because the exception is swallowed.

The screenshot is just a placeholder so I could submit the issue, it is of no relevance.

Images

Screenshot 2026-10-01 at 16.48.56.png

Lingua principale
C#
Stelle
4k
Fork
453
Merge medio
12h 54m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dlemstra/Magick.NET

Tutte le issue di dlemstra/Magick.NET

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.