HaishinKit.swift icon indicating copy to clipboard operation
HaishinKit.swift copied to clipboard

Crash at resize method call of Circular Buffer

Open ParvinderjitSF opened this issue 2 years ago • 8 comments

Describe the bug

Getting app crashed while streaming

To Reproduce

random crash

Expected behavior

It shouldn't crash

Version

v: 1.3.0

Smartphone info.

iPhone SE (2nd Gen) iOS: 16.0.0

Additional context

Log from the Crashlytic

Crashed: com.haishinkit.HaishinKit.NetSocket.output
0  Foundation                     0x41cacc Data.InlineSlice.replaceSubrange(:with:count:) + 224
1  Foundation                     0x4239a4 Data.Representation.replaceSubrange(:with:count:) + 672
2  HaishinKit                     0x62d04 NetSocket.CircularBuffer.resize((smiley) + 964 (<compiler-generated>:964)
3  HaishinKit                     0x616e0 closure #2 in NetSocket.doOutput(data:locked:) + 104
4  HaishinKit                     0x1ac5c thunk for @escaping @callee_guaranteed () -> () + 28 (<compiler-generated>:28)
5  libdispatch.dylib              0x24b4 _dispatch_call_block_and_release + 32
6  libdispatch.dylib              0x3fdc _dispatch_client_callout + 20
7  libdispatch.dylib              0xb694 _dispatch_lane_serial_drain + 672
8  libdispatch.dylib              0xc1e0 _dispatch_lane_invoke + 384
9  libdispatch.dylib              0x16e10 _dispatch_workloop_worker_thread + 652
10 libsystem_pthread.dylib        0xdf8 _pthread_wqthread + 288
11 libsystem_pthread.dylib        0xb98 start_wqthread + 8

Screenshots

No response

Relevant log output

Crashed: com.haishinkit.HaishinKit.NetSocket.output
0  Foundation                     0x41cacc Data.InlineSlice.replaceSubrange(:with:count:) + 224
1  Foundation                     0x4239a4 Data.Representation.replaceSubrange(:with:count:) + 672
2  HaishinKit                     0x62d04 NetSocket.CircularBuffer.resize((smiley) + 964 (<compiler-generated>:964)
3  HaishinKit                     0x616e0 closure #2 in NetSocket.doOutput(data:locked:) + 104
4  HaishinKit                     0x1ac5c thunk for @escaping @callee_guaranteed () -> () + 28 (<compiler-generated>:28)
5  libdispatch.dylib              0x24b4 _dispatch_call_block_and_release + 32
6  libdispatch.dylib              0x3fdc _dispatch_client_callout + 20
7  libdispatch.dylib              0xb694 _dispatch_lane_serial_drain + 672
8  libdispatch.dylib              0xc1e0 _dispatch_lane_invoke + 384
9  libdispatch.dylib              0x16e10 _dispatch_workloop_worker_thread + 652
10 libsystem_pthread.dylib        0xdf8 _pthread_wqthread + 288
11 libsystem_pthread.dylib        0xb98 start_wqthread + 8

ParvinderjitSF avatar Oct 26 '22 06:10 ParvinderjitSF

@ronhuang @pvinis @spllr @akolov Can anyone help me here I have to upload my build to Appstore. I will be very thankful you all

ParvinderjitSF avatar Oct 30 '22 18:10 ParvinderjitSF

I have not used this lib in about 5 years. I would suggest you try the latest version (maybe it's fixed), then try to debug and see where that Data.InlineSlice thing is coming from and with what data (maybe you're doing something wrong), otherwise find another library or try to make your own library by understanding and copying things from this library here.

good luck.

pvinis avatar Oct 30 '22 19:10 pvinis

usually crashes are not hat random once you investigate. try and test as little of your code as possible every time until you figure out if it's your code or the lib that's doing something wrong.

pvinis avatar Oct 30 '22 19:10 pvinis

That's pretty much top 1 crash for us as well. Most of crashes happen when there's less than 100MB RAM left

leo150 avatar Nov 01 '22 21:11 leo150

I have the same problem.

jexwang avatar Dec 29 '22 02:12 jexwang

I believe the range is out of bounds when calling replaceSubrange in some cases, thereby the crash on this line: self.data.replaceSubrange(capacity - head..<capacity - head + subdata.count, with: subdata) Specifically capacity - head + subdata.count is larger than self.data.count after calling skip then resize via append.

heestand-xyz avatar Jan 20 '23 02:01 heestand-xyz

First, application that is runnning device with iPhoneSE2, iPhon7, 8 and so on has a memory limit 2G. If your app’s memory usage is 1.90〜1.95G. And resize method call of Circular Buffer, may crash.

Workaround, you can use RTMPNWSocket by Network.framework. rtmpConneciton.requireNetworkFramework = true

This class dose’t use a Circular Buffer. If memory limit is a factor, I think it will be replaced by another crash.

shogo4405 avatar Jan 22 '23 12:01 shogo4405

It seems necessary to not prevent buffer over run. Default buffer size is 65535 bytes. It is not enough recently. I might want to consider to change BufferSize = (AudioBitrate + VideoBitrate) / 8 (in bytes)

shogo4405 avatar Jan 29 '23 05:01 shogo4405

Closes refs https://github.com/shogo4405/HaishinKit.swift/pull/1406

shogo4405 avatar Apr 06 '24 05:04 shogo4405