How to reproduce:

```sql
EXPLAIN SYNTAX
SELECT x
FROM 
(
    SELECT 1 AS x
    UNION DISTINCT
    SELECT 1
)

Query id: e777bc02-5d1b-4d49-ba89-4ebe31172387


Exception on client:
Code: 32. DB::Exception: Attempt to read after eof: while receiving packet from localhost:9000

```

ProfileEvents::increment(ProfileEvents::InsertQueryTimeMicroseconds, query_time);
                }
                element.query_duration_ms = info.elapsed_seconds * 1000;
                element.read_rows = info.read_rows;
                element.read_bytes = info.read_bytes;
                element.written_rows = info.written_rows;
                element.written_bytes = info.written_bytes;
                element.memory_usage = info.peak_memory_usage > 0 ? info.peak_memory_usage : 0;
                element.thread_ids = std::move(info.thread_ids);
                element.profile_counters = std::move(info.profile_counters);
            };
            
            auto finish_callback = [elem, &context, ast,
                 log_queries,
                 log_queries_min_type = settings.log_queries_min_type,
                 log_queries_min_query_duration_ms = settings.log_queries_min_query_duration_ms.totalMilliseconds(),
                 status_info_to_query_log
            ]
                (IBlockInputStream * stream_in, IBlockOutputStream * stream_out, QueryPipeline * query_pipeline) mutable
            {
                QueryStatus * process_list_elem = context.getProcessListElement();
                if (!process_list_elem)
                    return;
                
                CurrentThread::finalizePerformanceCounters();
                QueryStatusInfo info = process_list_elem->getInfo(true, context.getSettingsRef().log_profile_events);
                double elapsed_seconds = info.elapsed_seconds;
                elem.type = QueryLogElementType::QUERY_FINISH;