**Describe the bug**
We have clickhouse-server going down now and then when inserting large amounts of data via clickhouse-client with an uncaught exception: "Terminate called for uncaught exception:#012Code: 24, e.displayText() = DB::Exception: Cannot write to ostream at offset 7937". I see there are several PRs (#20464, #20981, #21305) trying to fix this kind of errors, but it looks like not all corner cases are handled. From the code, `finalize()` will just re-throw any nested exception and it will end up unhandled in the HTTP server.

**Does it reproduce on recent release?**
21.3.2.5

**How to reproduce**
* Which ClickHouse server version to use
21.3.2.5

**Expected behavior**
The error must not be fatal.

**Error message and/or stacktrace**
```
2021.03.24 03:46:09.707694 [ 23124 ] {} <Fatal> BaseDaemon: (version 21.3.2.5, build id: 293C622C5C9ED35A848F4B84070256EB217A812D) (from thread 64876) Terminate called for uncaught exception:#012Code: 24, e.displayText() = DB::Exception: Cannot write to ostream at offset 7937, Stack trace (when copying this message, always include the lines below):#012#0120. DB::WriteBufferFromOStream::nextImpl() @ 0x8a46fc0 in /usr/bin/clickhouse#0121. DB::WriteBufferFromHTTPServerResponse::nextImpl() @ 0xfccb800 in /usr/bin/clickhouse#0122. DB::WriteBufferFromHTTPServerResponse::finalize() @ 0xfccbdf2 in /usr/bin/clickhouse#0123. DB::WriteBufferFromHTTPServerResponse::~WriteBufferFromHTTPServerResponse() @ 0xfccc486 in /usr/bin/clickhouse#0124. DB::InterserverIOHTTPHandler::handleRequest(DB::HTTPServerRequest&, DB::HTTPServerResponse&) @ 0xfc4c32a in /usr/bin/clickhouse#0125. DB::HTTPServerConnection::run() @ 0xfcc460f in /usr/bin/clickhouse#0126. Poco::Net::TCPServerConnection::start() @ 0x1236ec7f in /usr/bin/clickhouse#0127. Poco::Net::TCPServerDispatcher::run() @ 0x12370691 in /usr/bin/clickhouse#0128. Poco::PooledThread::run() @ 0x124a6db9 in /usr/bin/clickhouse#0129. Poco::ThreadImpl::runnableEntry(void*) @ 0x124a2c1a in /usr/bin/clickhouse#01210. start_thread @ 0x7fa3 in /usr/lib/x86_64-linux-gnu/libpthread-2.28.so#01211. clone @ 0xf94cf in /usr/lib/x86_64-linux-gnu/libc-2.28.so#012 (version 21.3.2.5)
2021.03.24 03:46:09.726762 [ 71381 ] {} <Fatal> BaseDaemon: ########################################
2021.03.24 03:46:09.726927 [ 71381 ] {} <Fatal> BaseDaemon: (version 21.3.2.5, build id: 293C622C5C9ED35A848F4B84070256EB217A812D) (from thread 64876) (no query) Received signal Aborted (6)
2021.03.24 03:46:09.726944 [ 71381 ] {} <Fatal> BaseDaemon: 
2021.03.24 03:46:09.726965 [ 71381 ] {} <Fatal> BaseDaemon: Stack trace: 0x7fd6225807bb 0x7fd62256b535 0x8b093b8 0x13eb9163 0x13eb910c 0x894cf5b 0xfccc5ed 0xfc4c32a 0xfcc460f 0x1236ec7f 0x12370691 0x124a6db9 0x124a2c1a 0x7fd622711fa3 0x7fd6226424cf
2021.03.24 03:46:09.727074 [ 71381 ] {} <Fatal> BaseDaemon: 1. raise @ 0x377bb in /usr/lib/x86_64-linux-gnu/libc-2.28.so
2021.03.24 03:46:09.727085 [ 71381 ] {} <Fatal> BaseDaemon: 2. abort @ 0x22535 in /usr/lib/x86_64-linux-gnu/libc-2.28.so
2021.03.24 03:46:09.727163 [ 71381 ] {} <Fatal> BaseDaemon: 3. ? @ 0x8b093b8 in /usr/bin/clickhouse
2021.03.24 03:46:09.727246 [ 71381 ] {} <Fatal> BaseDaemon: 4. ? @ 0x13eb9163 in ?
2021.03.24 03:46:09.727278 [ 71381 ] {} <Fatal> BaseDaemon: 5. std::terminate() @ 0x13eb910c in ?
2021.03.24 03:46:09.727358 [ 71381 ] {} <Fatal> BaseDaemon: 6. ? @ 0x894cf5b in /usr/bin/clickhouse
2021.03.24 03:46:09.727382 [ 71381 ] {} <Fatal> BaseDaemon: 7. DB::WriteBufferFromHTTPServerResponse::~WriteBufferFromHTTPServerResponse() @ 0xfccc5ed in /usr/bin/clickhouse
2021.03.24 03:46:09.727406 [ 71381 ] {} <Fatal> BaseDaemon: 8. DB::InterserverIOHTTPHandler::handleRequest(DB::HTTPServerRequest&, DB::HTTPServerResponse&) @ 0xfc4c32a in /usr/bin/clickhouse
2021.03.24 03:46:09.727432 [ 71381 ] {} <Fatal> BaseDaemon: 9. DB::HTTPServerConnection::run() @ 0xfcc460f in /usr/bin/clickhouse
2021.03.24 03:46:09.727448 [ 71381 ] {} <Fatal> BaseDaemon: 10. Poco::Net::TCPServerConnection::start() @ 0x1236ec7f in /usr/bin/clickhouse
2021.03.24 03:46:09.727457 [ 71381 ] {} <Fatal> BaseDaemon: 11. Poco::Net::TCPServerDispatcher::run() @ 0x12370691 in /usr/bin/clickhouse
2021.03.24 03:46:09.727469 [ 71381 ] {} <Fatal> BaseDaemon: 12. Poco::PooledThread::run() @ 0x124a6db9 in /usr/bin/clickhouse
2021.03.24 03:46:09.727479 [ 71381 ] {} <Fatal> BaseDaemon: 13. Poco::ThreadImpl::runnableEntry(void*) @ 0x124a2c1a in /usr/bin/clickhouse
2021.03.24 03:46:09.727497 [ 71381 ] {} <Fatal> BaseDaemon: 14. start_thread @ 0x7fa3 in /usr/lib/x86_64-linux-gnu/libpthread-2.28.so
2021.03.24 03:46:09.727508 [ 71381 ] {} <Fatal> BaseDaemon: 15. clone @ 0xf94cf in /usr/lib/x86_64-linux-gnu/libc-2.28.so
2021.03.24 03:46:09.840498 [ 71381 ] {} <Fatal> BaseDaemon: Calculated checksum of the binary: DCFE6F75B6E72B76D731FF6621C84A85. There is no information about the reference checksum.
```
{
    temporary_buffer = Aws::MakeShared<Aws::StringStream>("temporary buffer");
    temporary_buffer->exceptions(std::ios::badbit);
    last_part_size = 0;
}
void WriteBufferFromS3::finalize()
{
    
    MemoryTracker::LockExceptionInThread lock;
    finalizeImpl();
}
void WriteBufferFromS3::finalizeImpl()
{
    if (finalized)
        return;
    next();
    if (multipart_upload_id.empty())
    {
        makeSinglepartUpload();
    }
    else
    {
        
        writePart();
        completeMultipartUpload();
    }
    finalized = true;
}
WriteBufferFromS3::~WriteBufferFromS3()