Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when load ipfs:// url while pinning an NFT #17670

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion components/ipfs/pin/ipfs_base_pin_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ IpfsBaseJob::IpfsBaseJob() = default;

IpfsBaseJob::~IpfsBaseJob() = default;

void IpfsBaseJob::Cancel() {
is_canceled_ = true;
}

IpfsBasePinService::IpfsBasePinService(IpfsService* ipfs_service)
: ipfs_service_(ipfs_service) {
ipfs_service_->AddObserver(this);
Expand All @@ -25,12 +29,16 @@ IpfsBasePinService::~IpfsBasePinService() = default;

void IpfsBasePinService::OnIpfsShutdown() {
daemon_ready_ = false;
if (current_job_) {
current_job_->Cancel();
current_job_.reset();
}
}

void IpfsBasePinService::OnGetConnectedPeers(
bool success,
const std::vector<std::string>& peers) {
if (success) {
if (success && !daemon_ready_) {
daemon_ready_ = true;
DoNextJob();
}
Expand Down
7 changes: 7 additions & 0 deletions components/ipfs/pin/ipfs_base_pin_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class IpfsBaseJob {
IpfsBaseJob();
virtual ~IpfsBaseJob();
virtual void Start() = 0;
virtual void Cancel();

protected:
bool is_canceled_ = false;
};

// Manages a queue of IpfsService-related tasks.
Expand All @@ -43,6 +47,9 @@ class IpfsBasePinService : public IpfsServiceObserver {
IpfsBasePinService();

private:
FRIEND_TEST_ALL_PREFIXES(IpfsBasePinServiceTest, OnGetConnectedPeers);
FRIEND_TEST_ALL_PREFIXES(IpfsBasePinServiceTest, OnIpfsShutdown);

bool IsDaemonReady();
void MaybeStartDaemon();
void OnDaemonStarted();
Expand Down
54 changes: 54 additions & 0 deletions components/ipfs/pin/ipfs_base_pin_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,58 @@ TEST_F(IpfsBasePinServiceTest, TasksExecuted) {
EXPECT_TRUE(second_method_called.value());
}

TEST_F(IpfsBasePinServiceTest, OnIpfsShutdown) {
service()->OnGetConnectedPeers(true, {});
EXPECT_TRUE(service()->daemon_ready_);

std::unique_ptr<MockJob> first_job =
std::make_unique<MockJob>(base::DoNothing());
std::unique_ptr<MockJob> second_job =
std::make_unique<MockJob>(base::DoNothing());

service()->AddJob(std::move(first_job));
service()->AddJob(std::move(second_job));

service()->OnIpfsShutdown();

EXPECT_EQ(1u, service()->jobs_.size());
EXPECT_FALSE(service()->current_job_);

service()->OnJobDone(false);

EXPECT_EQ(1u, service()->jobs_.size());
EXPECT_FALSE(service()->current_job_);
}

TEST_F(IpfsBasePinServiceTest, OnGetConnectedPeers) {
service()->OnGetConnectedPeers(true, {});
EXPECT_TRUE(service()->daemon_ready_);

std::unique_ptr<MockJob> first_job =
std::make_unique<MockJob>(base::DoNothing());
std::unique_ptr<MockJob> second_job =
std::make_unique<MockJob>(base::DoNothing());

service()->AddJob(std::move(first_job));

service()->OnGetConnectedPeers(true, {});

service()->AddJob(std::move(second_job));

service()->OnGetConnectedPeers(true, {});

EXPECT_EQ(1u, service()->jobs_.size());
EXPECT_TRUE(service()->current_job_);

service()->OnJobDone(true);

EXPECT_EQ(0u, service()->jobs_.size());
EXPECT_TRUE(service()->current_job_);

service()->OnJobDone(true);

EXPECT_EQ(0u, service()->jobs_.size());
EXPECT_FALSE(service()->current_job_);
}

} // namespace ipfs
15 changes: 15 additions & 0 deletions components/ipfs/pin/ipfs_local_pin_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ void AddLocalPinJob::Start() {
}

void AddLocalPinJob::OnAddPinResult(absl::optional<AddPinResult> result) {
if (is_canceled_) {
std::move(callback_).Run(false);
return;
}

if (!result) {
std::move(callback_).Run(false);
return;
Expand Down Expand Up @@ -117,6 +122,11 @@ void VerifyLocalPinJob::Start() {
}

void VerifyLocalPinJob::OnGetPinsResult(absl::optional<GetPinsResult> result) {
if (is_canceled_) {
std::move(callback_).Run(absl::nullopt);
return;
}

if (!result) {
std::move(callback_).Run(absl::nullopt);
return;
Expand Down Expand Up @@ -161,6 +171,11 @@ void GcJob::Start() {
}

void GcJob::OnGetPinsResult(absl::optional<GetPinsResult> result) {
if (is_canceled_) {
std::move(callback_).Run(false);
return;
}

if (!result) {
std::move(callback_).Run(false);
return;
Expand Down