diff --git a/src/main/java/io/muserver/handlers/ResourceProvider.java b/src/main/java/io/muserver/handlers/ResourceProvider.java
index <HASH>..<HASH> 100644
--- a/src/main/java/io/muserver/handlers/ResourceProvider.java
+++ b/src/main/java/io/muserver/handlers/ResourceProvider.java
@@ -198,11 +198,11 @@ class AsyncFileProvider implements ResourceProvider, CompletionHandler<Integer, 
     @Override
     public void sendTo(MuRequest request, MuResponse response, boolean sendBody, long maxLen) throws IOException {
         if (sendBody) {
+            this.maxLen = maxLen;
             handle = request.handleAsync();
             channel = AsynchronousFileChannel.open(localPath, StandardOpenOption.READ);
             buf = ByteBuffer.allocate(8192);
             channel.read(buf, curPos, handle, this);
-            this.maxLen = maxLen;
         } else {
             response.outputStream();
         }
Fixed async bug where occasionally a file download would just fail when writing the first bytes.

This happened because an important value was set after a listener was set up, and so occasionally the registered callback would be called before the important value was set.