Fix the send loop for a streaming proxy
Your task
Your media proxy sends each buffer with one write call and logs "short write" when the socket accepts fewer bytes than asked. It then reports success, so clients get truncated frames. The fix is a loop that keeps writing until every byte is sent. You model it with a script of how many bytes the socket accepts on each call.
An assistant wrote send_all_chunks(payload_len, accept_script). It is in the editor. It has a bug. Find it and fix it.
Input and output:
payload_len: how many bytes to send.accept_script: what each successivewritecall can accept, in order. A value of 0 or less means the call moved no bytes (the socket said "try again", or a signal interrupted it).- Return the number of bytes each successful write sent, in order.
Rules the tests check:
- If
payload_lenis 0 or less, return[]. - Keep writing while bytes remain. Each write uses the next script value.
- A write with a value of 0 or less sends nothing and is not recorded. The loop retries with the next value.
- A write with a positive value sends that many bytes, or what remains if that is fewer, and records the amount.
- Once the script runs out, the socket accepts everything: the next write sends all that remains.
- The recorded amounts always add up to
payload_len.
Examples
send_all_chunks(payload_len: int, accept_script: list[int]) -> list[int]Example 1
Input
send_all_chunks(10, [4, 4, 4])Output
[4, 4, 2]Example 2
Input
send_all_chunks(10, [10])Output
[10]Example 3
Input
send_all_chunks(10, [3, 0, 3, -1, 100])Output
[3, 3, 4]