Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

javascript - Error during writing to file: Assertion `args[3]->IsInt32()' failed

I'm trying to write a torrent client where when writing the received blocks to the file it is giving me this error. I have tried to implement multifile torrents and it is giving me exactly at the point where a single block belongs to multiple files and has to be split. I am unable to debug this problem and any help would be greatly appreciated!

const net = require('net');
const tracker = require('./tracker.js');
const Buffer = require('buffer').Buffer;
const message = require('./message.js');
const Pieces = require('./Pieces.js');
const Queue = require('./Queue.js');
const fs = require('fs');
const tp = require('./torrent-parser.js');
const cliProgress = require('cli-progress');
//for reference just in case
...
const fileHandler = fileHandler.js
...
//unnecesary code emmited, please let me know if required

function .... {
...
const files = fileHandler.initializeFiles(torrent);
...
}

function pieceHandler(socket, payload, pieces, queue, torrent, files){     
   
    console.log(payload);
    pieces.addReceived(payload);
    b1.increment({speed : getSpeed(pieces)});  //the progress bar library (cli-progress)

    let offset = payload.index*torrent.info['piece length'] + payload.begin;   
    let blockEnd = offset + payload.block.length;
    let fileDetails = fileHandler.chooseFile(files, offset, blockEnd);
    let start = 0;
    console.log(fileDetails);
    fs.write(fileDetails.index, payload.block.slice(start, start + fileDetails.length), 0, fileDetails.length, fileDetails.start, () => {});

    while(fileDetails.carryforward){  //When carryforward flag (block spanning multiple files) is active

        start += fileDetails.length;
        offset += fileDetails.length;
        fileDetails = fileHandler.chooseFile(files, offset);        
        fs.write(fileDetails.index, payload.block.slice(start, start + fileDetails.length), 0, fileDetails.length, fileDetails.start, () => {});

    }

This is the piece handler function which receives the incoming blocks of data sent from the peer, processes it and then writes to the appropriate file.

The pieces can be considered as one huge string of bytes and the offset is the position of the incoming block from 0. And since a single block can span multiple files I have to implement this code.

'use strict';

const fs = require('fs');
const path = require('path');
const tp = require('./torrent-parser.js')


module.exports.initializeFiles = (torrent) => {
    fs.mkdir(('./' + torrent.info.name.toString('utf8')), 
  { recursive: true }, (err) => { 
    if (err) { 
      return console.error(err); 
    } 
    
  }); 

    const files = [];
    var start = 0;
    
    torrent.info.files.forEach(item => {       
        const dict = {
            start : start,
            end : start + item.length-1,
            file : fs.openSync('./' + (torrent.info.name.toString('utf8') + '/' + item.path.toString('utf8')), 'w'),
            length : item.length
        }
        start = start + item.length;
        files.push(dict);
    });
   return files; 
};

module.exports.chooseFile = (files, offset, blockEnd) => {
  
  for(let i = 0; i < files.length; i++){
    if(files[i].end >= offset){ //confusion b/w > and >=
      const temp = (files[i].end - offset + 1);
      const left = blockEnd-offset;
      const carryforward = (temp < left ) ? true : false; //tells if block length exceeds current file length
      const length = (temp < left) ? temp : left;

      return {
        index : files[i].file,
        length : length,
        start : offset - files[i].start,
        carryforward : carryforward
      };
    }
  }
};

This is the fileHandler.js file which is responsible for giving the information required to know which incoming block is written to which file.

Error code:

{
  index: 0,
  begin: 0,
  block: <Buffer 0d 0a 31 0d 0a 30 30 3a 30 32 3a 32 30 2c 30 30 30 20 2d 2d 3e 20 30 30 3a 30 32 3a 32 31 2c 30 30 33 0d 0a 45 78 63 75 73 65 20 6d 65 3f 0d 0a 0d 0a ... 16334 more bytes>
} //incoming piece on which error occured

{ index: 3, length: 3945, start: 0, carryforward: true }  //console.log from fileHandler for debugging
      
C:WindowsSystem32cmd.exe - node  index.js[5356]: c:wssrc
ode_file.cc:1833: Assertion `args[3]->IsInt32()' failed.
 1: 00007FF6E65D021F napi_wrap+109311
 2: 00007FF6E6575286 v8::internal::OrderedHashTable<v8::internal::OrderedHashSet,1>::NumberOfElementsOffset+33302
 3: 00007FF6E6575601 v8::internal::OrderedHashTable<v8::internal::OrderedHashSet,1>::NumberOfElementsOffset+34193
 4: 00007FF6E656E804 v8::internal::OrderedHashTable<v8::internal::OrderedHashSet,1>::NumberOfElementsOffset+6036
 5: 00007FF6E6DED76F v8::internal::Builtins::builtin_handle+321471
 6: 00007FF6E6DECD04 v8::internal::Builtins::builtin_handle+318804
 7: 00007FF6E6DECFF7 v8::internal::Builtins::builtin_handle+319559
 8: 00007FF6E6DECE43 v8::internal::Builtins::builtin_handle+319123
 9: 00007FF6E6EC8FDD v8::internal::SetupIsolateDelegate::SetupHeap+464173
10: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
11: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
12: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
13: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
14: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
15: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
16: 00007FF6E6E5B519 v8::internal::SetupIsolateDelegate::SetupHeap+14953
17: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
18: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
19: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
20: 00007FF6E6E618E2 v8::internal::SetupIsolateDelegate::SetupHeap+40498
21: 00007FF6E6E5F59E v8::internal::SetupIsolateDelegate::SetupHeap+31470
22: 00007FF6E6E5F18C v8::internal::SetupIsolateDelegate::SetupHeap+30428
23: 00007FF6E6D2F671 v8::internal::Execution::CallWasm+1649
24: 00007FF6E6D2EEDF v8::internal::Execution::Call+191
25: 00007FF6E6E1A827 v8::Function::Call+615
26: 00007FF6E65F428B node::CallbackScope::~CallbackScope+1659
27: 00007FF6E65EBF64 v8::internal::compiler::Operator::EffectOutputCount+228
28: 00007FF6E649E4D3 v8::internal::Isolate::isolate_root_bias+13459
29: 00007FF6E649F057 v8::internal::Isolate::isolate_root_bias+16407
30: 00007FF6E6499CB9 v8::internal::MicrotaskQueue::microtasks_policy+1257
31: 00007FF6E660DF9B uv_tty_set_vterm_state+9211
32: 00007FF6E6623AFC uv_loop_init+924
33: 00007FF6E6623E0A uv_run+202
34: 00007FF6E652FD05 v8::internal::OrderedHashTable<v8::internal::OrderedHashSet,1>::NumberOfBucketsOffset+9365
35: 00007FF6E65A3567 node::Start+311
36: 00007FF6E640686C RC4_options+339820
37: 00007FF6E73A507C v8::internal::compiler::RepresentationChanger::Uint32OverflowOperatorFor+153532
38: 00007FFE13607C24 BaseThreadInitThunk+20
39: 00007FFE1382D4D1 RtlUserThreadStart+33

I'm new to programming and any inputs on the multifile writing algorithm would also be happily welcomed! Thank you very much!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Although your log output doesn't indicate that large offsets were used this still might be a recently fixed limitation where write only accepted 32bit offsets, i.e. it would fail on offsets larger than 4GB. You could try running your code on a recent nightly build of node js.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...