<commit_before>import Foundation

protocol Attempt: class, OperationConvertible {
    init(
        request: URLRequest,
        session: VKURLSession,
        callbacks: AttemptCallbacks
    )
}

final class AttemptImpl: Operation, Attempt {
    
    private let request: URLRequest
    private var task: VKURLSessionTask?
    private let urlSession: VKURLSession
    private let callbacks: AttemptCallbacks
    
    init(
        request: URLRequest,
        session: VKURLSession,
        callbacks: AttemptCallbacks
        ) {
        self.request = request
        self.urlSession = session
        self.callbacks = callbacks
        super.init()
    }
    
    override func main() {
        let semaphore = DispatchSemaphore(value: 0)
        
        let completion: (Data?, URLResponse?, Error?) -> () = { [weak self] data, response, error in
            defer {
                semaphore.signal()
            }
            
            guard let strongSelf = self, !strongSelf.isCancelled else { return }
            
            if let error = error {
                strongSelf.callbacks.onFinish(.error(.urlRequestError(error)))
            }
            else if let data = data {
                strongSelf.callbacks.onFinish(Response(data))
            }
            else {
                strongSelf.callbacks.onFinish(.error(.unexpectedResponse))
            }
        }
        
        task = urlSession.dataTask(with: request, completionHandler: completion)
        task?.addObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesReceived), options: .new, context: nil)
        task?.addObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesSent), options: .new, context: nil)
        task?.resume()
        
        semaphore.wait()
    }
    
    override func observeValue(
        forKeyPath keyPath: String?,
        of object: Any?,
        change: [NSKeyValueChangeKey: Any]?,
        context: UnsafeMutableRawPointer?
        ) {
        guard let keyPath = keyPath else { return }
        guard let task = task else { return }
        <commit_msg>[Core] Fix freeze when calling sleep() in callbacks<commit_after>import Foundation

protocol Attempt: class, OperationConvertible {
    init(
        request: URLRequest,
        session: VKURLSession,
        callbacks: AttemptCallbacks
    )
}

final class AttemptImpl: Operation, Attempt {
    
    private let request: URLRequest
    private var task: VKURLSessionTask?
    private let urlSession: VKURLSession
    private let callbacks: AttemptCallbacks
    
    init(
        request: URLRequest,
        session: VKURLSession,
        callbacks: AttemptCallbacks
        ) {
        self.request = request
        self.urlSession = session
        self.callbacks = callbacks
        super.init()
    }
    
    override func main() {
        let semaphore = DispatchSemaphore(value: 0)
        
        let completion: (Data?, URLResponse?, Error?) -> () = { [weak self] data, response, error in
            /// Because URLSession executes completions in their own serial queue
            DispatchQueue.global(qos: .utility).async {
                defer {
                    semaphore.signal()
                }
                
                guard let strongSelf = self, !strongSelf.isCancelled else { return }

                if let error = error {
                    strongSelf.callbacks.onFinish(.error(.urlRequestError(error)))
                }
                else if let data = data {
                    strongSelf.callbacks.onFinish(Response(data))
                }
                else {
                    strongSelf.callbacks.onFinish(.error(.unexpectedResponse))
                }
            }
        }
        
        task = urlSession.dataTask(with: request, completionHandler: completion)
        task?.addObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesReceived), options: .new, context: nil)
        task?.addObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesSent), options: .new, context: nil)
        task?.resume()
        
        semaphore.wait()
    }
    
    override func observeValue(
        forKeyPath keyPath: String?,
        of object: Any?,
        change: [NSKeyValueChangeKey: Any]?,
        context: UnsafeMutableRawPointer?
        ) {
        guard let keyPath = keyPath else { return }
        guard let task = task else { return }
        
{'fraction_non_alphanumeric': 0.08344640434192672, 'fraction_numerical': 0.0004522840343735866, 'mean_word_length': 1.902230971128609, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6533308', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1147, 'n_words': 288}