Repository: AvaloniaUI/Avalonia
File: src/Avalonia.X11/Clipboard/X11Clipboard.cs (C#)

Code before:
            while (true)
            {
                if (null == await events.WaitForEventAsync(x =>
                        x.type == XEventName.PropertyNotify && x.PropertyEvent.atom == property
                                                            && x.PropertyEvent.state == 1, TimeSpan.FromMinutes(1)))
                    break;
                var read = await data.ReadAsync(buffer, 0, buffer.Length);
                if(read == 0)
                    break;
                XChangeProperty(_x11.Display, window, property, target, 8, PropertyMode.Replace, buffer, read);
            }
            ArrayPool<byte>.Shared.Return(buffer);

            // Finish the transfer
            XChangeProperty(_x11.Display, window, property, target, 8, PropertyMode.Replace, IntPtr.Zero, 0);
        }

        void SendDataToClient(IntPtr window, IntPtr property, IntPtr target, byte[] bytes)
        {
            if (bytes.Length < _maximumPropertySize)
            {
                XChangeProperty(_x11.Display, window, property, target, 8,
                    PropertyMode.Replace,
                    bytes, bytes.Length);
            }
            SendIncrDataToClient(window, property, target, new MemoryStream(bytes));
        }

        private bool HasOwner => XGetSelectionOwner(_x11.Display, _x11.Atoms.CLIPBOARD) != IntPtr.Zero;
        
        private ClipboardReadSession OpenReadSession() => new(_platform);

        public async Task<string?> GetTextAsync()
        {
            if (!HasOwner)
                return null;
            if (TryGetInProcessDataObject() is { } inProc)
                return inProc.GetText();
            
            using var session = OpenReadSession();
            var res = await session.SendFormatRequest();
            var target = _x11.Atoms.UTF8_STRING;
            if (res != null)
            {
                var preferredFormats = new[] {_x11.Atoms.UTF16_STRING, _x11.Atoms.UTF8_STRING, _x11.Atoms.XA_STRING};
                foreach (var pf in preferredFormats)
                    if (res.Contains(pf))
                    {
                        target = pf;
                        break;
                    }

Review comment (suggestion):
Is an `else` missing? This will currently set the property twice (once normally, once with INCR) if the data doesn't need INCR.

Code after:
            while (true)
            {
                if (null == await events.WaitForEventAsync(x =>
                        x.type == XEventName.PropertyNotify && x.PropertyEvent.atom == property
                                                            && x.PropertyEvent.state == 1, TimeSpan.FromMinutes(1)))
                    break;
                var read = await data.ReadAsync(buffer, 0, buffer.Length);
                if(read == 0)
                    break;
                XChangeProperty(_x11.Display, window, property, target, 8, PropertyMode.Replace, buffer, read);
            }
            ArrayPool<byte>.Shared.Return(buffer);

            // Finish the transfer
            XChangeProperty(_x11.Display, window, property, target, 8, PropertyMode.Replace, IntPtr.Zero, 0);
        }

        void SendDataToClient(IntPtr window, IntPtr property, IntPtr target, byte[] bytes)
        {
            if (bytes.Length < _maximumPropertySize)
            {
                XChangeProperty(_x11.Display, window, property, target, 8,
                    PropertyMode.Replace,
                    bytes, bytes.Length);
            }
            else
                SendIncrDataToClient(window, property, target, new MemoryStream(bytes));
        }

        private bool HasOwner => XGetSelectionOwner(_x11.Display, _x11.Atoms.CLIPBOARD) != IntPtr.Zero;
        
        private ClipboardReadSession OpenReadSession() => new(_platform);

        public async Task<string?> GetTextAsync()
        {
            if (!HasOwner)
                return null;
            if (TryGetInProcessDataObject() is { } inProc)
                return inProc.GetText();
            
            using var session = OpenReadSession();
            var res = await session.SendFormatRequest();
            var target = _x11.Atoms.UTF8_STRING;
            if (res != null)
            {
                var preferredFormats = new[] {_x11.Atoms.UTF16_STRING, _x11.Atoms.UTF8_STRING, _x11.Atoms.XA_STRING};
                foreach (var pf in preferredFormats)
                    if (res.Contains(pf))
                    {
                        target = pf;
                        break;