Rushaan
Rushaan
CC#
Created by Rushaan on 4/21/2025 in #help
✅ Memory issue in ASP.NET Core video playback endpoint
also I noticed for some reason the bytes requested in Range header implied it requested for full video at once, the value of Range header was often like "bytes=73007104-"
8 replies
CC#
Created by Rushaan on 4/21/2025 in #help
✅ Memory issue in ASP.NET Core video playback endpoint
public async Task ExecuteResultAsync(ActionContext context)
{
var response = context.HttpContext.Response;
response.ContentType = _contentType;
response.Headers["Accept-Ranges"] = "bytes";
response.Headers["Connection"] = "close";

var poolBuf = ArrayPool<byte>.Shared.Rent(512 * 1024);

try
{
await using var fs = new FileStream(
_path, FileMode.Open, FileAccess.Read, FileShare.Read,
bufferSize: poolBuf.Length, useAsync: true);

long total = fs.Length;
var rangeHeader = context.HttpContext.Request.Headers["Range"].ToString();
var openEnded = rangeHeader.EndsWith("-", StringComparison.Ordinal);

var (start, intendedEnd) = ParseRange(context.HttpContext.Request, total);

var end = openEnded
? Math.Min(start + poolBuf.Length - 1, total - 1)
: intendedEnd;

var length = end - start + 1;
response.StatusCode = (start == 0 && end == total - 1) ? 200 : 206;
response.Headers["Content-Range"] = $"bytes {start}-{end}/{total}";
response.Headers["Content-Length"] = length.ToString();

fs.Seek(start, SeekOrigin.Begin);
var ct = context.HttpContext.RequestAborted;
long remaining = length;

while (remaining > 0 && !ct.IsCancellationRequested)
{
int toRead = (int)Math.Min(poolBuf.Length, remaining);
int read = await fs.ReadAsync(poolBuf, 0, toRead, ct);
if (read == 0) break;

await response.Body.WriteAsync(poolBuf, 0, read, ct);
await response.Body.FlushAsync(ct);
remaining -= read;
}
}
finally
{
ArrayPool<byte>.Shared.Return(poolBuf);
}
}
public async Task ExecuteResultAsync(ActionContext context)
{
var response = context.HttpContext.Response;
response.ContentType = _contentType;
response.Headers["Accept-Ranges"] = "bytes";
response.Headers["Connection"] = "close";

var poolBuf = ArrayPool<byte>.Shared.Rent(512 * 1024);

try
{
await using var fs = new FileStream(
_path, FileMode.Open, FileAccess.Read, FileShare.Read,
bufferSize: poolBuf.Length, useAsync: true);

long total = fs.Length;
var rangeHeader = context.HttpContext.Request.Headers["Range"].ToString();
var openEnded = rangeHeader.EndsWith("-", StringComparison.Ordinal);

var (start, intendedEnd) = ParseRange(context.HttpContext.Request, total);

var end = openEnded
? Math.Min(start + poolBuf.Length - 1, total - 1)
: intendedEnd;

var length = end - start + 1;
response.StatusCode = (start == 0 && end == total - 1) ? 200 : 206;
response.Headers["Content-Range"] = $"bytes {start}-{end}/{total}";
response.Headers["Content-Length"] = length.ToString();

fs.Seek(start, SeekOrigin.Begin);
var ct = context.HttpContext.RequestAborted;
long remaining = length;

while (remaining > 0 && !ct.IsCancellationRequested)
{
int toRead = (int)Math.Min(poolBuf.Length, remaining);
int read = await fs.ReadAsync(poolBuf, 0, toRead, ct);
if (read == 0) break;

await response.Body.WriteAsync(poolBuf, 0, read, ct);
await response.Body.FlushAsync(ct);
remaining -= read;
}
}
finally
{
ArrayPool<byte>.Shared.Return(poolBuf);
}
}
8 replies
CC#
Created by Rushaan on 4/21/2025 in #help
✅ Memory issue in ASP.NET Core video playback endpoint
well I've tried this and it didn't really help, identical memory rising behaviour as in the above code
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 64 * 1024, useAsync: true);
Response.Headers["X-Accel-Buffering"] = "no";
return File(stream, mimeType, true);
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 64 * 1024, useAsync: true);
Response.Headers["X-Accel-Buffering"] = "no";
return File(stream, mimeType, true);
I also tried returning new FileStreamResult but same thing I also tried returning this by creating new class & implementing IActionResult with method below but literally got identical memory rising behavior as I described in the post:
8 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
btw I forgot to confirm but its working as expected now and unit tests are still passing
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
oh
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
actually if I remember correctly in the assertion part of unit test I did not assert the updated child paths so its possible unit test has no issue
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
its alr I think it has pretty much same issue thats why it was passing but first imma test out the actual function
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
tysm I think it will fix it. PS: That means theres very similar fault in unit test of that function too
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
so await UpdateChildPaths(folder, previousFolderPath, newFolderPathOfFolder); should be enough to fix the issue right
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
ohhhhhhhhhhhhhh
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
help pls my brain feels slightly cooked
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
js realized i need to sleep rn but id appreciate any help:)
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
Also if the updated child folder's path does not miss the folder being moved's name at its appropriate place in the path then the updated child folder's path would be entirely correct which is also the goal of fixing this issue
15 replies
CC#
Created by Rushaan on 12/11/2024 in #help
Logical flaw in code
Also apart from that its doing what its supposed to do and the updated path of the folder being moved is correct
15 replies
CC#
Created by Rushaan on 12/9/2024 in #help
Collection was modified. Enumeration operation may not execute.
imma test it a lil more, yep its fully working
16 replies
CC#
Created by Rushaan on 12/9/2024 in #help
Collection was modified. Enumeration operation may not execute.
damn it worked tysm
16 replies
CC#
Created by Rushaan on 12/9/2024 in #help
Collection was modified. Enumeration operation may not execute.
alr imma try it
16 replies
CC#
Created by Rushaan on 12/9/2024 in #help
Collection was modified. Enumeration operation may not execute.
okay, EF Core will still track the changes right?
16 replies
CC#
Created by Rushaan on 12/9/2024 in #help
Collection was modified. Enumeration operation may not execute.
oh, the line the error is pointing to seems to be the first foreach loop
16 replies
CC#
Created by Rushaan on 12/9/2024 in #help
Collection was modified. Enumeration operation may not execute.
yea
16 replies